Every Swing user interface component (except some containers and dialog boxes, such as JPanel , JSplitPane , JFileChooser , and JColorChooser ) has a property named model that refers to its data model. The data model is defined in an interface whose name ends with Model . For example, the model for button component is ButtonModel . Most model interfaces have a default implementation class that is commonly named DefaultX , where X is its model interface name. For example, the default implementation class for ButtonModel is DefaultButtonModel . The relationship of a Swing component, its model interface, and its default model implementation class is illustrated in Figure 30.6.
For convenience, most Swing components 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. For example, the properties actionCommand and mnemonic are defined in both ButtonModel and JButton . Actually, these properties are in the AbstractButton class. Since JButton is a subclass of AbstractButton , it inherits all the properties from AbstractButton .
When you create a Swing component without specifying a model, a default data model is assigned to the model property. For example, the following code sets the actionCommand and mnemonic properties of a button through its model:
public class TestSwingModel1 { public static void main(String[] args) { javax.swing.JButton jbt = new javax.swing.JButton();
// Obtain the default model from the component javax.swing.ButtonModel model = jbt.getModel(); // Set properties in the model model.setActionCommand( "OK" ); model.setMnemonic( 'O' ); // Display the property values from the component System.out.println( "actionCommand is " + jbt.getActionCommand()); System.out.println( "mnemonic is " + ( char )(jbt.getMnemonic())); } }
The output is
actionCommand is OK mnemonic is O
You can also create a new model and assign it to a Swing component. For example, the following code creates an instance of ButtonModel and assigns it to an instance of JButton :
public class TestSwingModel2 { public static void main(String[] args) { javax.swing.JButton jbt = new javax.swing.JButton(); // Create a new button model javax.swing.ButtonModel model = new javax.swing.DefaultButtonModel(); // Set properties in the model model.setActionCommand( "OK" ); model.setMnemonic( 'O' ); // Assign the model to the button jbt.setModel(model); // Display the property values from the component System.out.println( "actionCommand is " + jbt.getActionCommand()); System.out.println( "mnemonic is " + jbt.getMnemonic()); } }
It is unnecessary to use the models for simple Swing components, such as JButton , JToggleButton , JCheckBox , JRadioButton , JTextField , and JTextArea , because the frequently used properties in their models are also in these components. You can access and modify these properties directly through the components. For advanced components, such as JSpinner , JList , JComboBox , JTable , and JTree , you have to work with their models to store, access, and modify data.