










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











JComponent Model UI Component class is what applications typically interact with directly. Model and UI classes are typically “hidden” by the component, and used internally by it.
(^) Model: responsible for storing the state of your component (^) Reuse an existing model if one is suitable; create your own if not (^) 1. Create an interface for your model and an implementation class, if you’re defining a new one (^) Decide on the data structures you’ll need to track, and create getter/setter functions (^) Called Properties if they match the standard Java-style standards (^) 2. Send PropertyChangeEvents (or just ChangeEvents) when data in the model change (^) This means you’ll need to keep a list of PropertyChangeListeners (or just ChangeListeners), and provide methods for adding and removing listeners (^) EventListenerList can help with this (^) Be careful: the model should only contain core data structures, not data that’s only about the visual presentation of that data (^) Example: a Scrollbar (^) Minimum, maximum, and current values are model properties (they have to do with actual data values, not display (^) 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)
(^) 1. Design the component’s external API (^) These are the methods that application programmers see and use (^) Many will just forward to the underlying model or the UI (^) 2. Make your component a listener for the Model’s ChangeEvents or PropertyChangeEvents (^) Generally need to call repaint() whenever the model is updated (^) 3. Send PropertyChangeEvents if the component’s internal state changes (^) Other components might be listening to you--send PropertyChangeEvents if anything component- specific changes (^) 4. Implement some boilerplate methods to register models and UIs (^) public void setUI(); (^) public void updateUI(); (^) Used to set the UI, and change it on the fly (^) public String getUIClassID(); (^) Should return whatever the UI_CLASS_ID string is for “compatible” UIs for this component (^) public void setModel(); (^) public Model getModel(); (^) Used to set and return the model. When the your model is set, your component should register itself as a listener for the model’s change events.
JComponent implements ChangeListener Model Component has reference to model Model sends ChangeEvents to Component In setModel() method of Component:
JComponent UI implements MouseListener, etc. In paint() method:
public class NotepageComponent extends JComponent implements ChangeListener { NotepageModel model; public NotepageComponent() { setModel(new NotepageModel()); updateUI(); } public setModel(NotepageModel m) { if (model != null) model.removeChangeListener(this); model = m; model.addChangeListener(this); } public NotepageModel getModel() { return model; } public void setUI(NotepageUI ui) { super.setUI(ui); } public void updateUI() { setUI((NotepageUI) UIManager.getUI(this)); invalidate(); } public String getUIClassID() { return NotepageUI.UI_CLASS_ID; } }