Principles CS 6456
Popular in Course
Popular in ComputerScienence
This 0 page Class Notes was uploaded by Alayna Veum on Monday November 2, 2015. The Class Notes belongs to CS 6456 at Georgia Institute of Technology - Main Campus taught by Warren Edwards in Fall. Since its upload, it has received 22 views. For similar materials see /class/234058/cs-6456-georgia-institute-of-technology-main-campus in ComputerScienence at Georgia Institute of Technology - Main Campus.
Reviews for Principles
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 11/02/15
am 004939 0000 Using MVC with Swing 2 Components Georgia Tech My E Jumping Ahead a Bit Georgia Tech 390 We re going to cover a speci c architectural approach to building UI components ModelView Controller Classic architecture from Smalltalk 80 0 Model data structures that represent the component s state 0 View object responsible for drawing the component Q Controller object responsible for responding to user input Why talk about it now Swing optionally allows a modi ed version of MVC as a way for building components I d like you to use this approach for Homework 2 Georgia 000 Tech 390 Some Swing History 0 Remember from earlier in class 0 To create a new component subclass JComponent 0 Implement paintComponent to do all of the drawing for your component 0 Nice easy way to create components 0 Still works ne 0 But makes some things very hard Q How would you implement a new lookandfeel 9 Components drawing code is hard coded into them 6 Even if you had a big switch statement and implemented several look and feels still doesn t help you if a new look and feel comes along Some Swing History cont d O O O O 0000 Georgia 000 Tech 390 Swing has a pluggable look and feel architecture PLAF Supports Windows Mac GTK plus several Javaonly LAFs To make these easier to use many Swing components have factored their implementations in a slightly different way 0 Separation of the underlying component data from its look and behavior Allows you to create just a new lookandfeel for a component and easily plug it in to work with the core component data Georgia 0 o o Component Internal Architecture JComponent Component class is what applications typically interact with directly Model and UI classes are typically hidden by the component and used internally by it Georgia 000 Tech Swing MVC Overview 0 Model custom class that contains all of the internal state of a component Q Ul custom class that handles user input events and painting the component 0 Subsumes both theView and Controller from the classic MVC architecture These two classes are looselycoupled 0 They communicate with each other through events 0 Eg when something in the model updates it sends a ChangeEvent to whatever Ul is associated with it 0 UI then calls repaint to tell the RepaintManager to schedule it for redrawing Georgia 000 Tech Swing MVC Overview 0 Application programmers typically never see the UI or the Model classes Q Used purely as an internal implementation feature of the component 0 Requires a bit of structure and boilerplate code to make things work right 0 Resources 0 Short overview article MVC Meets Swing linked off class website U Book last chapter covers creating new Swing components using this architecture 0 0 O oooo Georgla coo Tech 0 Step CreateYour Model Class 0 Model responsible for storing the state of your component 0 Reuse an existing model if one is suitable create your own if not 0 Create an interface for your model and an implementation class if you re de ning a new one Q Decide on the data structures you ll need to track and create gettersetter functions 0 Called Properties if they match the standard Javastyle standards 0 2 Send PropertyChangeEvents or just ChangeEvents when data in the model change Q This means you ll need to keep a list of PropertyChangeListeners or just ChangeListeners and provide methods for adding and removing listeners Q EventListenerList can help with this 0 Be careful the model should only contain core data structures not data that s only about the visual presentation of that data 0 Example a Scrollbar 0 Minimum maximum and current values are model properties they have to do with actual data values not display 0 Whether tick marks are shown labels etc are visual properties and don t belong in the model they re only about display not the actual data Step 2 Create an Abstract Ul Class Georgia Tech 0 This is an abstract superclass to be shared by all Lan for your new component 0 Will be the superclass of all Uls that are compatible with your new component for this phase of the project there will be only one class that subclasses it 0 Always follows the same basic format import javaxswingpafComponentUI public abstract class NotepageUl extends ComponentUl public static nal String UCLASSD NotepageUl Step 3 Create the Actual Ul 3 Georgia 000 Class Techl 39 O l Extend your abstract Ul class 2 Implement public void paintGraphics gJComponent c 0 Your component will automatically delegate its drawing to your Ul s paint method 0 3 Implement any interfaces you need in order to respond to input events 0 Example if your component must respond to the mouse have your UI class implement MouseListenerYou ll tell the component to send any mouse events to your UI to be handled there 0 4 Draw yourself correctly given your current size Q Recall that your parent component may resize you In your painting code use the current size getWidthlgetHeight and draw in the space alloted to you 0 5 Implement a bit of boilerplate code for Ul management public static ComponentUl createUlJComponent c 0 Create and return an instance of your UI class 9 public void installUlGComponent c O Register this Ul instance as the listener for the component s input events 0 public void uninstallUlGComponent c O Unregister this Ul instance as the listener for the component s input events 10 Step 4 Create the Component Itself Ge 5 0 Design the component s external API 0 These are the methods that application programmers see and use Q Many will just forward to the underlying model or the UI 0 2 Make your component a listener for the Model s ChangeEvents or PropertyChangeEvents 0 Generally need to call repaint whenever the model is updated 0 3 Send PropertyChangeEvents if the component s internal state changes 393 Other components might be listening to yousend PropertyChangeEvents if anything component specific changes 0 4 Implement some boilerplate methods to register models and Uls Q public void setUl 0 public void updateUl 0 Used to set the UI and change it on the fly 0 public String getUlClasle 0 Should return whatever the UCLASSD string is for compatible Uls for this component public void setModel 0 public Model getModel 0 Used to set and return the modelWhen the your model is set your component should register itself as a listener for the model s change events 11 Step 5 Register your UI with Getzsa Swing s UlManager 0 Need to tell the UlManager about the speci c Ul you want to use 0 Typically do this early in the application s main routine public static void mainString args UlManagerputPhotoUUCLASSD BasicNotepageUl other stuff here This string serves as the This string names the class unique token identifying all that implements the specific different Uls that work as lookandfeel UI you want to use NotepageUIs in this application 12 Georgia coo Component Internal Architecture JComponent implements ChangeListener In setModel method of Component Component registers itself as a ChangeListener for the model Whenever ChangeEvent is received from model Component calls repaint to cause itself to be redrawn 13 Georgia coo Component Internal Architecture JComponent In installUl method Ul sets itself up as mousekeyboardetc listener for the component When user events come in Ul updates the model by calling out to the component In paint method Component is passed in to paint Ask component for data that needs to be drawn Ul does not have a reference to the model Ul does not have a reference to the but accesses indirectly through the model but accesses indirectly Component through the Component 14 Georgia 000 Tech 390 7 Step 3 example public class BasicNotepageUl extends NotepageUl implements MouseListener public static ComponentUl createUlGComponent c return new BasicNotepageUl public void installUlUComponent c NotepageComponent caddMouseListenerthis we ll handle mouse events for the Notepage component public void uninstallUlUComponent c NotepageComponent cremoveMouseListenerthis public void paintGraphics gJComponent c do painting for the component here implement the various MouseListener methods 15 Geesgall Step 4 Example 3 public class NotepageComponent extends JComponent implements ChangeListener NotepageModel model public NotepageComponentO setModeInew NotepageModel updateUl public setModeINotepageModel m if model null modelremoveChangeListenerthis model m modeladdChangeListenerthis public NotepageModel getModel return model public void setUlNotepageUl ui supersetUlui public void updateUl setUlNotepageUl UlManagergetUlthis invalidate public String getUlClasle return NotepageUlUlCLASSlD 16 Georgia 000 Tech 390 Common Problems 0 Exceptions at startup time 0 Make sure the UlManager registration is done before you use the component 0 Components aren t being repainted all the time 6 Make sure you re registered for change events and are calling repaint whenever anything changes 0 Components come up at weird sizes 0 Your component should provide a miminumSize and preferredSize when it is requested If you don t do this your parent may set your size to O 17
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'