Building applications should be data-centric, not user-centric, which is a good programming practice. To support this programming paradigm, swing defines a separate model interface for each component with logical data or values, which allows the program to choose to embed its own model implementation into the swing component.
The following table lists the mapping relationships for components and their models in swing:
Component |
Model interface |
Model type |
JButton |
Buttonmodel |
GUI State |
Jtogglebutton |
Buttonmodel |
GUI State/Application Data |
Jcheckbox |
Buttonmodel |
GUI State/Application Data |
Jradiobutton |
Buttonmodel |
GUI State/Application Data |
JMenu |
Buttonmodel |
GUI State |
JMenuItem |
Buttonmodel |
GUI State |
JCheckBoxMenuItem |
Buttonmodel |
GUI State/Application Data |
JRadioButtonMenuItem |
Buttonmodel |
GUI State/Application Data |
JComboBox |
ComboBoxModel |
Application Data |
JProgressBar |
BoundedRangeModel |
GUI State/Application Data |
Jscrollbar |
BoundedRangeModel |
GUI State/Application Data |
JSlider |
BoundedRangeModel |
GUI State/Application Data |
JTabbedPane |
Singleselectionmodel |
GUI State |
JList |
Listmodel |
Application Data |
JList |
ListSelectionModel |
GUI State |
JTable |
TableModel |
Application Data |
JTable |
Tablecolumnmodel |
GUI State |
JTree |
TreeModel |
Application Data |
JTree |
Treeselectionmodel |
GUI State |
JEditorPane |
Document |
Application Data |
Jtextpane |
Document |
Application Data |
JTextArea |
Document |
Application Data |
JTextField |
Document |
Application Data |
JPasswordField |
Document |
Application Data |
Swing Model classification
The models offered by swing fall into two broad categories: the GUI state model and the application data model.
The GUI state model is an interface that describes the visual state of a GUI control, such as whether a button is pressed or if the item in the list is selected. GUI state models are typically used only in graphical user interface (GUI) environments. In general, although it is useful to write a GUI state model to detach a program, especially if multiple GUI controls share state, or when manipulating a control to automatically update another's value, the GUI state model is not required in swing, and it is entirely possible to manipulate the state of the GUI control through the top-level method of the component. Without having to interact directly with the model.
The application data model is an interface that describes data that has an application meaning, such as data in a table, or an option to display a list. These data models provide swing with a powerful programming model for clearly dividing the application interface and data logic. For data-focused swing components, such as JTree and JTable, it is highly recommended that the data model be used for interaction.
Of course, some component models are classified between the GUI state model and the application data model according to the different application scenarios, such as the BoundedRangeModel of JSlider and JProgressBar.
The separable model interface of swing does not clearly define the GUI state model and the application data model. So here's how you do this, so that you better understand when and why you need to use a separate model.