The fundamental issue in the model-view approach is to ensure consistency between the views and the model. Any change in the model should be notified to the dependent views, and all the views should display the same data consistently. The model can be implemented as a source with appropriate event and event listener registration methods . The view can be implemented as a listener. Thus, if data are changed in the model, the view will be notified.
Every Swing user interface component (e.g., JButton , JTextField , JList , and JComboBox ) has a property named model that refers to its data model. The data model is defined in an interface whose name ends with Model (e.g., SpinnerModel , ListModel , ComboBoxModel , TableModel , and TreeModel ).
Most simple Swing components (e.g., JButton , JTextField , JTextArea ) contain some properties of their models, and these properties can be accessed and modified directly from the component without knowing the existence of the model.
A JSpinner is displayed as a text field with a pair of tiny arrow buttons on its right side that enable the user to select numbers , dates, or values from an ordered sequence. A JSpinner 's sequence value is defined by the SpinnerModel interface. AbstractSpinnerModel is a convenient abstract class that implements SpinnerModel and provides the implementation for its registration/deregistration methods. SpinnerListModel , SpinnerNumberModel , and SpinnerDateModel are concrete implementations of SpinnerModel . SpinnerNumberModel represents a sequence of numbers with properties maximum , minimum , and stepSize . SpinnerDateModel represents a sequence of dates. SpinnerListModel can store a list of any object values.
A JSpinner has a single child component, called the editor , which is responsible for displaying the current element or value of the model. Four editors are defined as static inner classes inside JSpinner : JSpinner.DefaultEditor , JSpinner.NumberEditor , JSpinner.DateEditor , and JSpinner.ListEditor .
JList has two supporting models: a list model and a list-selection model. The list model is for storing and processing data. The list-selection model is for selecting items. By default, items are rendered as strings or icons. You can also create a custom renderer implementing the ListCellRenderer interface.
JComboBox delegates the responsibilities of storing and maintaining data to its data model. All combo box models implement the ComboBoxModel interface, which extends the ListModel interface and defines the getSelectedItem and setSelectedItem methods for retrieving and setting a selected item. The methods for adding and removing items are defined in the MutableComboBoxModel interface, which extends ComboBoxModel . When an instance of JComboBox is created without explicitly specifying a model, an instance of DefaultComboBoxModel is used. The DefaultComboBoxModel class extends AbstractListModel and implements MutableComboBoxModel .
Combo boxes render cells exactly like lists, because the combo box items are displayed in a list contained in a popup menu. Therefore, a combo box cell renderer can be created exactly like a list cell renderer by implementing the ListCellRenderer interface.