public class

DefaultButtonModel

extends Object
implements Serializable ButtonModel
java.lang.Object
   ↳ javax.swing.DefaultButtonModel
Known Direct Subclasses

Class Overview

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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

Summary

Constants
int ARMED Identifies the "armed" bit in the bitmask, which indicates partial commitment towards choosing/triggering the button.
int ENABLED Identifies the "enabled" bit in the bitmask, which indicates that the button can be selected by an input device (such as a mouse pointer).
int PRESSED Identifies the "pressed" bit in the bitmask, which indicates that the button is pressed.
int ROLLOVER Identifies the "rollover" bit in the bitmask, which indicates that the mouse is over the button.
int SELECTED Identifies the "selected" bit in the bitmask, which indicates that the button has been selected.
Fields
protected String actionCommand The action command string fired by the button.
protected ChangeEvent changeEvent Only one ChangeEvent is needed per button model instance since the event's only state is the source property.
protected ButtonGroup group The button group that the button belongs to.
protected EventListenerList listenerList Stores the listeners on this model.
protected int mnemonic The button's mnemonic.
protected int stateMask The bitmask used to store the state of the button.
Public Constructors
DefaultButtonModel()
Constructs a DefaultButtonModel.
Public Methods
void addActionListener(ActionListener l)
Adds an ActionListener to the model.
void addChangeListener(ChangeListener l)
Adds a ChangeListener to the model.
void addItemListener(ItemListener l)
Adds an ItemListener to the model.
String getActionCommand()
Returns the action command string for the button.
ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this DefaultButtonModel.
ChangeListener[] getChangeListeners()
Returns an array of all the change listeners registered on this DefaultButtonModel.
ButtonGroup getGroup()
Returns the group that the button belongs to.
ItemListener[] getItemListeners()
Returns an array of all the item listeners registered on this DefaultButtonModel.
<T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this model.
int getMnemonic()
Gets the keyboard mnemonic for the button.
Object[] getSelectedObjects()
Overridden to return null.
boolean isArmed()
Indicates partial commitment towards triggering the button.
boolean isEnabled()
Indicates if the button can be selected or triggered by an input device, such as a mouse pointer.
boolean isPressed()
Indicates if the button is pressed.
boolean isRollover()
Indicates that the mouse is over the button.
boolean isSelected()
Indicates if the button has been selected.
void removeActionListener(ActionListener l)
Removes an ActionListener from the model.
void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the model.
void removeItemListener(ItemListener l)
Removes an ItemListener from the model.
void setActionCommand(String actionCommand)
Sets the action command string that gets sent as part of the ActionEvent 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 state
void setSelected(boolean b)
Selects or deselects the button.
Protected Methods
void fireActionPerformed(ActionEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void fireItemStateChanged(ItemEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void fireStateChanged()
Notifies all listeners that have registered interest for notification on this event type.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.awt.ItemSelectable
From interface javax.swing.ButtonModel

Constants

public static final int ARMED

Identifies the "armed" bit in the bitmask, which indicates partial commitment towards choosing/triggering the button.

Constant Value: 1 (0x00000001)

public static final int ENABLED

Identifies the "enabled" bit in the bitmask, which indicates that the button can be selected by an input device (such as a mouse pointer).

Constant Value: 8 (0x00000008)

public static final int PRESSED

Identifies the "pressed" bit in the bitmask, which indicates that the button is pressed.

Constant Value: 4 (0x00000004)

public static final int ROLLOVER

Identifies the "rollover" bit in the bitmask, which indicates that the mouse is over the button.

Constant Value: 16 (0x00000010)

public static final int SELECTED

Identifies 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.

Constant Value: 2 (0x00000002)

Fields

protected String actionCommand

The action command string fired by the button.

protected ChangeEvent changeEvent

Only one ChangeEvent is needed per button model instance since the event's only state is the source property. The source of events generated is always "this".

protected ButtonGroup group

The button group that the button belongs to.

protected EventListenerList listenerList

Stores the listeners on this model.

protected int mnemonic

The button's mnemonic.

protected int stateMask

The bitmask used to store the state of the button.

Public Constructors

public DefaultButtonModel ()

Constructs a DefaultButtonModel.

Public Methods

public void addActionListener (ActionListener l)

Adds an ActionListener to the model.

Parameters
l the listener to add

public void addChangeListener (ChangeListener l)

Adds a ChangeListener to the model.

Parameters
l the listener to add

public void addItemListener (ItemListener l)

Adds an ItemListener to the model.

Parameters
l the listener to add

public String getActionCommand ()

Returns the action command string for the button.

Returns
  • the String that identifies the generated event

public ActionListener[] getActionListeners ()

Returns an array of all the action listeners registered on this DefaultButtonModel.

Returns
  • all of this model's ActionListeners or an empty array if no action listeners are currently registered

public ChangeListener[] getChangeListeners ()

Returns an array of all the change listeners registered on this DefaultButtonModel.

Returns
  • all of this model's ChangeListeners or an empty array if no change listeners are currently registered

public ButtonGroup getGroup ()

Returns the group that the button belongs to. Normally used with radio buttons, which are mutually exclusive within their group.

Returns
  • the ButtonGroup that the button belongs to

public ItemListener[] getItemListeners ()

Returns an array of all the item listeners registered on this DefaultButtonModel.

Returns
  • all of this model's ItemListeners or an empty array if no item listeners are currently registered

public T[] getListeners (Class<T> listenerType)

Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a DefaultButtonModel instance m 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.

Parameters
listenerType the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns
  • an array of all objects registered as FooListeners on this model, or an empty array if no such listeners have been added
Throws
ClassCastException if listenerType doesn't specify a class or interface that implements java.util.EventListener

public int getMnemonic ()

Gets the keyboard mnemonic for the button.

Returns
  • an int specifying the accelerator key

public Object[] getSelectedObjects ()

Overridden to return null.

public boolean isArmed ()

Indicates partial commitment towards triggering the button.

Returns
  • true if the button is armed, and ready to be triggered

public boolean isEnabled ()

Indicates if the button can be selected or triggered by an input device, such as a mouse pointer.

Returns
  • true if the button is enabled

public boolean isPressed ()

Indicates if the button is pressed.

Returns
  • true if the button is pressed

public boolean isRollover ()

Indicates that the mouse is over the button.

Returns
  • true if the mouse is over the button

public boolean isSelected ()

Indicates if the button has been selected. Only needed for certain types of buttons - such as radio buttons and check boxes.

Returns
  • true if the button is selected

public void removeActionListener (ActionListener l)

Removes an ActionListener from the model.

Parameters
l the listener to remove

public void removeChangeListener (ChangeListener l)

Removes a ChangeListener from the model.

Parameters
l the listener to remove

public void removeItemListener (ItemListener l)

Removes an ItemListener from the model.

Parameters
l the listener to remove

public void setActionCommand (String actionCommand)

Sets the action command string that gets sent as part of the ActionEvent when the button is triggered.

Parameters
actionCommand the String that identifies the generated event

public void setArmed (boolean b)

Marks the button as armed or unarmed.

Parameters
b whether or not the button should be armed

public void setEnabled (boolean b)

Enables or disables the button.

Parameters
b whether or not the button should be enabled

public void setGroup (ButtonGroup group)

Identifies the group the button belongs to -- needed for radio buttons, which are mutually exclusive within their group.

Parameters
group the ButtonGroup the button belongs to

public void setMnemonic (int key)

Sets the keyboard mnemonic (shortcut key or accelerator key) for the button.

Parameters
key an int specifying the accelerator key

public void setPressed (boolean b)

Sets the button to pressed or unpressed.

Parameters
b whether or not the button should be pressed

public void setRollover (boolean b)

Sets or clears the button's rollover state

Parameters
b whether or not the button is in the rollover state

public void setSelected (boolean b)

Selects or deselects the button.

Parameters
b true selects the button, false deselects the button

Protected Methods

protected void fireActionPerformed (ActionEvent e)

Notifies all listeners that have registered interest for notification on this event type.

Parameters
e the ActionEvent to deliver to listeners

protected void fireItemStateChanged (ItemEvent e)

Notifies all listeners that have registered interest for notification on this event type.

Parameters
e the ItemEvent to deliver to listeners

protected void fireStateChanged ()

Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.