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.
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
Property | Data type | get | is | set | Default value |
---|---|---|---|---|---|
actionCommand | String | · | · | ||
armed | boolean | · | · | ||
enabled | boolean | · | · | ||
group | ButtonGroup | · | |||
mnemonic | int | · | · | ||
pressed | boolean | · | · | ||
rollover | boolean | · | · | ||
selected | boolean | · | · | ||
See also |
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 ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.