Chapter 4. Labels and Icons
4.2.1 The Icon Interface
4.2.2.6 Protected Method
protected void loadImage(Image image)
Called by the constructors and setImage(). It uses a java.awt.MediaTracker object to
load an image synchronously. On completion, the image is guaranteed to be completely loaded, unless an error occurred, in which case the imageLoadStatus property will reflect
5.1 Buttons
Buttons are simple UI components used to generate events when the user presses them. In AWT, buttons were very basic, able to display only simple text strings. In much the same way as JLabel
provided improvements over java.awt.Label, the Swing button classes improve on
java.awt.Button and java.awt.Checkbox by introducing the ability to display icons, text, or
both. In this section, we'll introduce both the ButtonModel interface and DefaultButtonModel
class (which define the state of the button). Next, we'll look at the AbstractButton class (which
defines much of the functionality for all button types). Finally, we'll look at four concrete subclasses of AbstractButton and see how they can be grouped together using a ButtonGroup.
Figure 5.1 shows the class hierarchy, with significant relationships between the button-related Swing classes. Notice that, as we discussed in the introductory chapters, each button
(AbstractButton) keeps a reference to a ButtonModel, which represents its state.
Figure 5.1. Swing Button class diagram
The JMenuItem class shown here (and its subclasses, not shown) is not covered in this chapter.
Instead, they are covered in Chapter 14.
5.1.1 The ButtonModel Interface
The state of any Swing button is maintained by a ButtonModel object. This interface defines
methods for reading and writing the model's properties and for adding and removing various types of event listeners.
5.1.1.1 Properties
The properties for the ButtonModel interface are listed in Table 5.1. The actionCommand property
specifies the name of the command to be sent as part of the ActionEvent fired when the button is
which button has been pressed. The group property contains a reference to the ButtonGroup that
contains the button (if any).
Table 5.1, ButtonModel Properties
Property Data Type get is set bound Default Value
actionCommand String armed boolean enabled boolean group ButtonGroup mnemonic int pressed boolean rollover boolean selected boolean
See also java.awt.ItemSelectable.
mnemonic contains the key that can be pressed in conjunction with the L&F-specific modifier key in
order to produce the same effect as clicking the button with the mouse. The modifier key is currently the ALT key for all Swing L&Fs.
mnemonic is an integer property. However, a setMnemonic() method that takes a char is defined in AbstractButton. If you use the version that takes an int, specifying a lowercase character will cause the character to appear underlined, but will not result in the expected action when the key is pressed. Even if the character appears in lowercase on the button, the uppercase letter should be used as the mnemonic. Typically, it makes more sense to use the char method, unless you are working with the model directly.
The other properties are boolean flags that reflect certain aspects of the button's state. These flags
are defined by the constants in Table 5.4. The properties are:
armed
Indicates whether or not releasing the button causes an action to be performed. This becomes false when a button is pressed and the cursor is moved away from the button
while the mouse button is still being held down.
enabled
Indicates whether or not the button is currently enabled. A button must be enabled to be pressed.
pressed
Indicates whether or not the button is currently being pressed (meaning that the button is being held down).
rollover
Indicates whether or not the mouse cursor is currently over the button. This allows an alternate image to be displayed.
Indicates whether or not the button is currently selected. Only JToggleButton and its
subclasses may be selected. This property toggles on and off each time the button is clicked.
5.1.1.2 Events
Objects implementing the ButtonModel interface fire action events, change events, and item events,
as shown in Table 5.2.
Table 5.2, ButtonModel Events
Event Description ActionEvent The button has been pressed.
ChangeEvent A change has occurred in one or more properties of the button model.
ItemEvent The button has been toggled on or off.
The ButtonModel interface contains the following standard methods for maintaining event
subscribers:
public abstract void addActionListener(ActionListener l) public abstract void removeActionListener(ActionListener l) public abstract void addItemListener(ItemListener l)
public abstract void removeItemListener(ItemListener l) public abstract void addChangeListener(ChangeListener l) public abstract void removeChangeListener(ChangeListener l)
5.1.2 The DefaultButtonModel Class
Swing provides a default implementation of the ButtonModel interface called
DefaultButtonModel. This class is used directly by the JButton class. The other button classes
(JToggleButton and its descendants) use an extension of DefaultButtonModel (defined as an
inner class within JToggleButton) to handle their state data.
5.1.2.1 Properties
The DefaultButtonModel class gets most of its properties from ButtonModel. The default values
set by this class are shown in Table 5.3.
Table 5.3, DefaultButtonModel Properties
Property Data Type get is set bound Default Value
actionCommand* String null
armed* boolean false
enabled* boolean true
group* ButtonGroup null
mnemonic* int 0
pressed* boolean false
rollover* boolean false
selected* boolean false
The only property here that does not come from the ButtonModel interface is the
selectedObjects property. This property comes as part of the ItemSelectable interface
implemented by this class. The accessor method always returns null.
5.1.2.2 Events
The events fired by DefaultButtonModel are those required by ButtonModel and listed in Table
5-2. An ActionEvent is fired when the button is pressed, an ItemEvent is fired when the button's
state is changed, and a ChangeEvent is fired when a change has occurred in the button's properties.
The following methods are implemented in this class using the EventListenerList class:
public void addActionListener(ActionListener l) public void removeActionListener(ActionListener l) public void addItemListener(ItemListener l)
public void removeItemListener(ItemListener l) public void addChangeListener(ChangeListener l) public void removeChangeListener(ChangeListener l)
In addition, the following protected methods are added in this class:
protected void fireActionPerformed(ActionEvent e)
protected void fireItemStateChanged(ItemEvent e)
protected void fireStateChanged()
These methods use the standard EventListenerList to dispatch events to registered
listeners. ChangeEvents are fired when the values of any of the following properties are set: armed, enabled, selected, pressed, rollover, mnemonic.