5.1 The ButtonModel Interface
The state of any Swing button is
maintained by a ButtonModel object. This interface
defines methods for reading and writing a model's
properties and for adding and removing various types of event
listeners.
5.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 that
is fired when the button is pressed. This can be used by event
handlers that are listening to multiple buttons to determine which
button is pressed.
Table 5-1. ButtonModel properties
actionCommand
|
String
|
·
|
|
·
|
|
armed
|
boolean
|
|
·
|
·
|
|
enabled
|
boolean
|
|
·
|
·
|
|
group
|
ButtonGroup
|
|
|
·
|
|
mnemonic
|
int
|
·
|
|
·
|
|
pressed
|
boolean
|
|
·
|
·
|
|
rollover
|
boolean
|
|
·
|
·
|
|
selected
|
boolean
|
|
·
|
·
|
|
See also java.awt.ItemSelectable.
|
If no
actionCommand is specified, an
ActionEvent takes the button's
text for its command string, so it is usually not necessary to
specify an explicit
actionCommand. You may find it useful to do so
for buttons that have icons but no text or for multiple buttons with
the same text. actionCommand properties can also
be handy for internationalization. For example, if you need to change
a button's text from "Hot" to
"Caliente", you won't have to
change any event-handling code if you set the
actionCommand to "Hot".
The group
property refers to the
ButtonGroup that contains the button (if any).
mnemonic contains the key that can be
pressed in conjunction with a 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.
 |
The type of the mnemonic property is
int because its value is intended to be one of the
VK_ "virtual
keycode" constants defined in
java.awt.KeyEvent (see Table 27-6). However, a setMnemonic( )
method that takes a char is defined in
AbstractButton. It's usually
easier to call setMnemonic('a') than it is to call
setMnemonic(KeyEvent.VK_A) unless you are working
with the model directly. If you use the char
version, it doesn't matter if you specify an
uppercase or lowercase character.
|
|
The other properties are boolean flags that
reflect certain aspects of the button's
state. The properties are:
- armed
-
Indicates whether releasing the
button causes an action to be performed. This becomes
false if the cursor is moved away from the button
while the mouse button is still being held down.
- enabled
-
Indicates whether the button is
currently enabled. A button must be enabled to be pressed.
- pressed
-
Indicates whether the button is
currently being pressed (meaning that the button is being held down).
- rollover
-
Indicates whether the mouse cursor
is currently over the button. This allows an alternate image to be
displayed.
- selected
-
Indicates whether the button is
currently selected. This is used only by
JToggleButton and its subclasses. This property
toggles between true and false
each time the button is clicked.
5.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
ActionEvent
|
The button is pressed.
|
ChangeEvent
|
A change has occurred in one or more properties of the button model.
|
ItemEvent
|
The button is toggled on or off.
|
The ButtonModel interface contains the following
standard methods for maintaining event subscribers:
- 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)
|