java.lang.Object
javax.swing.DefaultButtonModel
- All Implemented Interfaces:
ItemSelectable
,Serializable
,ButtonModel
- Direct Known Subclasses:
JToggleButton.ToggleButtonModel
The default implementation of a
Button
component's data model.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans
package.
Please see XMLEncoder
.
- Since:
- 1.2
-
Field Summary
Modifier and TypeFieldDescriptionprotected String
The action command string fired by the button.static final int
Identifies the "armed" bit in the bitmask, which indicates partial commitment towards choosing/triggering the button.protected ChangeEvent
Only oneChangeEvent
is needed per button model instance since the event's only state is the source property.static final int
Identifies the "enabled" bit in the bitmask, which indicates that the button can be selected by an input device (such as a mouse pointer).protected ButtonGroup
The button group that the button belongs to.protected EventListenerList
Stores the listeners on this model.protected int
The button's mnemonic.static final int
Identifies the "pressed" bit in the bitmask, which indicates that the button is pressed.static final int
Identifies the "rollover" bit in the bitmask, which indicates that the mouse is over the button.static final int
Identifies the "selected" bit in the bitmask, which indicates that the button has been selected.protected int
The bitmask used to store the state of the button. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds anActionListener
to the model.void
Adds aChangeListener
to the model.void
Adds anItemListener
to the model.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.Returns the action command string for the button.Returns an array of all the action listeners registered on thisDefaultButtonModel
.Returns an array of all the change listeners registered on thisDefaultButtonModel
.getGroup()
Returns the group that the button belongs to.Returns an array of all the item listeners registered on thisDefaultButtonModel
.<T extends EventListener>
T[]getListeners
(Class<T> listenerType) Returns an array of all the objects currently registered asFooListener
s upon this model.int
Gets the keyboard mnemonic for the button.Object[]
Overridden to returnnull
.boolean
isArmed()
Indicates partial commitment towards triggering the button.boolean
Indicates if the button can be selected or triggered by an input device, such as a mouse pointer.boolean
Indicates if the button is pressed.boolean
Indicates that the mouse is over the button.boolean
Indicates if the button has been selected.void
Removes anActionListener
from the model.void
Removes aChangeListener
from the model.void
Removes anItemListener
from the model.void
setActionCommand
(String actionCommand) Sets the action command string that gets sent as part of theActionEvent
when the button is triggered.void
setArmed
(boolean b) Marks the button as armed or unarmed.void
setEnabled
(boolean b) Enables or disables the button.void
setGroup
(ButtonGroup group) Identifies the group the button belongs to -- needed for radio buttons, which are mutually exclusive within their group.void
setMnemonic
(int key) Sets the keyboard mnemonic (shortcut key or accelerator key) for the button.void
setPressed
(boolean b) Sets the button to pressed or unpressed.void
setRollover
(boolean b) Sets or clears the button's rollover statevoid
setSelected
(boolean b) Selects or deselects the button.
-
Field Details
-
stateMask
protected int stateMaskThe bitmask used to store the state of the button. -
actionCommand
The action command string fired by the button. -
group
The button group that the button belongs to. -
mnemonic
protected int mnemonicThe button's mnemonic. -
changeEvent
Only oneChangeEvent
is needed per button model instance since the event's only state is the source property. The source of events generated is always "this". -
listenerList
Stores the listeners on this model. -
ARMED
public static final int ARMEDIdentifies the "armed" bit in the bitmask, which indicates partial commitment towards choosing/triggering the button.- See Also:
-
SELECTED
public static final int SELECTEDIdentifies the "selected" bit in the bitmask, which indicates that the button has been selected. Only needed for certain types of buttons - such as radio button or check box.- See Also:
-
PRESSED
public static final int PRESSEDIdentifies the "pressed" bit in the bitmask, which indicates that the button is pressed.- See Also:
-
ENABLED
public static final int ENABLEDIdentifies the "enabled" bit in the bitmask, which indicates that the button can be selected by an input device (such as a mouse pointer).- See Also:
-
ROLLOVER
public static final int ROLLOVERIdentifies the "rollover" bit in the bitmask, which indicates that the mouse is over the button.- See Also:
-
-
Constructor Details
-
DefaultButtonModel
public DefaultButtonModel()Constructs aDefaultButtonModel
.
-
-
Method Details
-
setActionCommand
Sets the action command string that gets sent as part of theActionEvent
when the button is triggered.- Specified by:
setActionCommand
in interfaceButtonModel
- Parameters:
actionCommand
- theString
that identifies the generated event- See Also:
-
getActionCommand
Returns the action command string for the button.- Specified by:
getActionCommand
in interfaceButtonModel
- Returns:
- the
String
that identifies the generated event - See Also:
-
isArmed
public boolean isArmed()Indicates partial commitment towards triggering the button.- Specified by:
isArmed
in interfaceButtonModel
- Returns:
true
if the button is armed, and ready to be triggered- See Also:
-
isSelected
public boolean isSelected()Indicates if the button has been selected. Only needed for certain types of buttons - such as radio buttons and check boxes.- Specified by:
isSelected
in interfaceButtonModel
- Returns:
true
if the button is selected
-
isEnabled
public boolean isEnabled()Indicates if the button can be selected or triggered by an input device, such as a mouse pointer.- Specified by:
isEnabled
in interfaceButtonModel
- Returns:
true
if the button is enabled
-
isPressed
public boolean isPressed()Indicates if the button is pressed.- Specified by:
isPressed
in interfaceButtonModel
- Returns:
true
if the button is pressed
-
isRollover
public boolean isRollover()Indicates that the mouse is over the button.- Specified by:
isRollover
in interfaceButtonModel
- Returns:
true
if the mouse is over the button
-
setArmed
public void setArmed(boolean b) Marks the button as armed or unarmed.- Specified by:
setArmed
in interfaceButtonModel
- Parameters:
b
- whether or not the button should be armed
-
setEnabled
public void setEnabled(boolean b) Enables or disables the button.- Specified by:
setEnabled
in interfaceButtonModel
- Parameters:
b
- whether or not the button should be enabled- See Also:
-
setSelected
public void setSelected(boolean b) Selects or deselects the button.- Specified by:
setSelected
in interfaceButtonModel
- Parameters:
b
-true
selects the button,false
deselects the button
-
setPressed
public void setPressed(boolean b) Sets the button to pressed or unpressed.- Specified by:
setPressed
in interfaceButtonModel
- Parameters:
b
- whether or not the button should be pressed- See Also:
-
setRollover
public void setRollover(boolean b) Sets or clears the button's rollover state- Specified by:
setRollover
in interfaceButtonModel
- Parameters:
b
- whether or not the button is in the rollover state- See Also:
-
setMnemonic
public void setMnemonic(int key) Sets the keyboard mnemonic (shortcut key or accelerator key) for the button.- Specified by:
setMnemonic
in interfaceButtonModel
- Parameters:
key
- an int specifying the accelerator key
-
getMnemonic
public int getMnemonic()Gets the keyboard mnemonic for the button.- Specified by:
getMnemonic
in interfaceButtonModel
- Returns:
- an int specifying the accelerator key
- See Also:
-
addChangeListener
Adds aChangeListener
to the model.- Specified by:
addChangeListener
in interfaceButtonModel
- Parameters:
l
- the listener to add
-
removeChangeListener
Removes aChangeListener
from the model.- Specified by:
removeChangeListener
in interfaceButtonModel
- Parameters:
l
- the listener to remove
-
getChangeListeners
Returns an array of all the change listeners registered on thisDefaultButtonModel
.- Returns:
- all of this model's
ChangeListener
s or an empty array if no change listeners are currently registered - Since:
- 1.4
- See Also:
-
fireStateChanged
protected void fireStateChanged()Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.- See Also:
-
addActionListener
Adds anActionListener
to the model.- Specified by:
addActionListener
in interfaceButtonModel
- Parameters:
l
- the listener to add
-
removeActionListener
Removes anActionListener
from the model.- Specified by:
removeActionListener
in interfaceButtonModel
- Parameters:
l
- the listener to remove
-
getActionListeners
Returns an array of all the action listeners registered on thisDefaultButtonModel
.- Returns:
- all of this model's
ActionListener
s or an empty array if no action listeners are currently registered - Since:
- 1.4
- See Also:
-
fireActionPerformed
Notifies all listeners that have registered interest for notification on this event type.- Parameters:
e
- theActionEvent
to deliver to listeners- See Also:
-
addItemListener
Adds anItemListener
to the model.- Specified by:
addItemListener
in interfaceButtonModel
- Specified by:
addItemListener
in interfaceItemSelectable
- Parameters:
l
- the listener to add- See Also:
-
removeItemListener
Removes anItemListener
from the model.- Specified by:
removeItemListener
in interfaceButtonModel
- Specified by:
removeItemListener
in interfaceItemSelectable
- Parameters:
l
- the listener to remove- See Also:
-
getItemListeners
Returns an array of all the item listeners registered on thisDefaultButtonModel
.- Returns:
- all of this model's
ItemListener
s or an empty array if no item listeners are currently registered - Since:
- 1.4
- See Also:
-
fireItemStateChanged
Notifies all listeners that have registered interest for notification on this event type.- Parameters:
e
- theItemEvent
to deliver to listeners- See Also:
-
getListeners
Returns an array of all the objects currently registered asFooListener
s upon this model.FooListener
s are registered using theaddFooListener
method.You can specify the
listenerType
argument with a class literal, such asFooListener.class
. For example, you can query aDefaultButtonModel
instancem
for its action listeners with the following code:ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));
If no such listeners exist, this method returns an empty array.- Type Parameters:
T
- the type of requested listeners- Parameters:
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
- Returns:
- an array of all objects registered as
FooListener
s on this model, or an empty array if no such listeners have been added - Throws:
ClassCastException
- iflistenerType
doesn't specify a class or interface that implementsjava.util.EventListener
- Since:
- 1.3
- See Also:
-
getSelectedObjects
Overridden to returnnull
.- Specified by:
getSelectedObjects
in interfaceItemSelectable
- Returns:
- the list of selected objects, or
null
-
setGroup
Identifies the group the button belongs to -- needed for radio buttons, which are mutually exclusive within their group.- Specified by:
setGroup
in interfaceButtonModel
- Parameters:
group
- theButtonGroup
the button belongs to
-
getGroup
Returns the group that the button belongs to. Normally used with radio buttons, which are mutually exclusive within their group.- Specified by:
getGroup
in interfaceButtonModel
- Returns:
- the
ButtonGroup
that the button belongs to - Since:
- 1.3
-