javax.swing
Class DefaultButtonModel

java.lang.Object
  extended by javax.swing.DefaultButtonModel
All Implemented Interfaces:
ItemSelectable, Serializable, ButtonModel
Direct Known Subclasses:
JToggleButton.ToggleButtonModel

public class DefaultButtonModel
extends Object
implements ButtonModel, Serializable

The default implementation of ButtonModel. The purpose of this class is to model the dynamic state of an abstract button. The concrete button type holding this state may be a a "toggle" button (checkbox, radio button) or a "push" button (menu button, button). If the model is disabled, only the "selected" property can be changed. An attempt to change the "armed", "rollover" or "pressed" properties while the model is disabled will be blocked. Any successful (non-blocked) change to the model's properties will trigger the firing of a ChangeEvent. Any change to the "selected" property will trigger the firing of an ItemEvent in addition to ChangeEvent. This is true whether the model is enabled or not. One other state change is special: the transition from "enabled, armed and pressed" to "enabled, armed and not-pressed". This is considered the "trailing edge" of a successful mouse click, and therefore fires an ActionEvent in addition to a ChangeEvent. In all other respects this class is just a container of boolean flags.

See Also:
Serialized Form

Field Summary
protected  String actionCommand
          The string used as the "command" property of any ActionEvent this model sends.
static int ARMED
          Indicates that the button is partially committed to being pressed, but not entirely.
protected  ChangeEvent changeEvent
          The single ChangeEvent this model (re)uses to call its ChangeListeners.
static int ENABLED
          State constant indicating that the button is enabled.
protected  ButtonGroup group
          The group this model belongs to.
protected  EventListenerList listenerList
          List of ItemListeners, ChangeListeners, and ActionListeners registered on this model.
protected  int mnemonic
          The key code (one of KeyEvent VK_) used to press this button via a keyboard interface.
static int PRESSED
          State constant indicating that the user is holding down the button.
static int ROLLOVER
          State constant indicating that the mouse is currently positioned over the button.
static int SELECTED
          State constant indicating that the button is selected.
protected  int stateMask
          Represents the "state properties" (armed, enabled, pressed, rollover and selected) by a bitwise combination of integer constants.
 
Constructor Summary
DefaultButtonModel()
          Creates a new DefaultButtonModel object.
 
Method Summary
 void addActionListener(ActionListener l)
          Add an ActionListener to the model.
 void addChangeListener(ChangeListener l)
          Add a ChangeListener to the model.
 void addItemListener(ItemListener l)
          Add an ItemListener to the model.
protected  void fireActionPerformed(ActionEvent e)
          Inform each ActionListener in the listenerList that an ActionEvent has occurred.
protected  void fireItemStateChanged(ItemEvent e)
          Inform each ItemListener in the listenerList that an ItemEvent has occurred.
protected  void fireStateChanged()
          Inform each ChangeListener in the listenerList that a ChangeEvent has occurred.
 String getActionCommand()
          Returns the current value of the model's "actionCommand" property.
 ActionListener[] getActionListeners()
          Returns all registered ActionListener objects.
 ChangeListener[] getChangeListeners()
          Returns all registered ChangeListener objects.
 ButtonGroup getGroup()
          Returns the current value of the model's "group" property.
 ItemListener[] getItemListeners()
          Returns all registered ItemListener objects.
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          Returns a specified class of listeners.
 int getMnemonic()
          Get the value of the model's "mnemonic" property.
 Object[] getSelectedObjects()
          Return null.
 boolean isArmed()
          Get the value of the model's "armed" property.
 boolean isEnabled()
          Get the value of the model's "enabled" property.
 boolean isPressed()
          Get the value of the model's "pressed" property.
 boolean isRollover()
          Get the value of the model's "rollover" property.
 boolean isSelected()
          Get the value of the model's "selected" property.
 void removeActionListener(ActionListener l)
          Remove an ActionListener to the model.
 void removeChangeListener(ChangeListener l)
          Remove a ChangeListener to the model.
 void removeItemListener(ItemListener l)
          Remove an ItemListener to the model.
 void setActionCommand(String s)
          Set the value of the model's "actionCommand" property.
 void setArmed(boolean a)
          Set the value of the model's "armed" property.
 void setEnabled(boolean e)
          Set the value of the model's "enabled" property.
 void setGroup(ButtonGroup g)
          Set the value of the model's "group" property.
 void setMnemonic(int key)
          Set the value of the model's "mnemonic" property.
 void setPressed(boolean p)
          Set the value of the model's "pressed" property.
 void setRollover(boolean r)
          Set the value of the model's "rollover" property.
 void setSelected(boolean s)
          Set the value of the model's "selected" property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARMED

public static final int ARMED
Indicates that the button is partially committed to being pressed, but not entirely. This usually happens when a user has pressed but not yet released the mouse button.

See Also:
Constant Field Values

ENABLED

public static final int ENABLED
State constant indicating that the button is enabled. Buttons cannot be pressed or selected unless they are enabled.

See Also:
Constant Field Values

PRESSED

public static final int PRESSED
State constant indicating that the user is holding down the button. When this transitions from true to false, an ActionEvent may be fired, depending on the value of the "armed" property.

See Also:
Constant Field Values

ROLLOVER

public static final int ROLLOVER
State constant indicating that the mouse is currently positioned over the button.

See Also:
Constant Field Values

SELECTED

public static final int SELECTED
State constant indicating that the button is selected. This constant is only meaningful for toggle-type buttons (radio buttons, checkboxes).

See Also:
Constant Field Values

stateMask

protected int stateMask
Represents the "state properties" (armed, enabled, pressed, rollover and selected) by a bitwise combination of integer constants.


listenerList

protected EventListenerList listenerList
List of ItemListeners, ChangeListeners, and ActionListeners registered on this model.


changeEvent

protected ChangeEvent changeEvent
The single ChangeEvent this model (re)uses to call its ChangeListeners.


group

protected ButtonGroup group
The group this model belongs to. Only one button in a group may be selected at any given time.


mnemonic

protected int mnemonic
The key code (one of KeyEvent VK_) used to press this button via a keyboard interface.


actionCommand

protected String actionCommand
The string used as the "command" property of any ActionEvent this model sends.

Constructor Detail

DefaultButtonModel

public DefaultButtonModel()
Creates a new DefaultButtonModel object.

Method Detail

getSelectedObjects

public Object[] getSelectedObjects()
Return null. Use AbstractButton if you wish to interface with a button via an ItemSelectable interface.

Specified by:
getSelectedObjects in interface ItemSelectable
Returns:
null

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns a specified class of listeners.

Parameters:
listenerType - the type of listener to return
Returns:
array of listeners

addActionListener

public void addActionListener(ActionListener l)
Add an ActionListener to the model. Usually only called to subscribe an AbstractButton's listener to the model.

Specified by:
addActionListener in interface ButtonModel
Parameters:
l - The listener to add
See Also:
ButtonModel.removeActionListener(ActionListener)

removeActionListener

public void removeActionListener(ActionListener l)
Remove an ActionListener to the model. Usually only called to unsubscribe an AbstractButton's listener to the model.

Specified by:
removeActionListener in interface ButtonModel
Parameters:
l - The listener to remove
See Also:
ButtonModel.addActionListener(ActionListener)

getActionListeners

public ActionListener[] getActionListeners()
Returns all registered ActionListener objects.

Returns:
array of ActionListener objects

addItemListener

public void addItemListener(ItemListener l)
Add an ItemListener to the model. Usually only called to subscribe an AbstractButton's listener to the model.

Specified by:
addItemListener in interface ItemSelectable
Specified by:
addItemListener in interface ButtonModel
Parameters:
l - The listener to add
See Also:
ButtonModel.removeItemListener(ItemListener)

removeItemListener

public void removeItemListener(ItemListener l)
Remove an ItemListener to the model. Usually only called to unsubscribe an AbstractButton's listener to the model.

Specified by:
removeItemListener in interface ItemSelectable
Specified by:
removeItemListener in interface ButtonModel
Parameters:
l - The listener to remove
See Also:
ButtonModel.removeItemListener(ItemListener)

getItemListeners

public ItemListener[] getItemListeners()
Returns all registered ItemListener objects.

Returns:
array of ItemListener objects

addChangeListener

public void addChangeListener(ChangeListener l)
Add a ChangeListener to the model. Usually only called to subscribe an AbstractButton's listener to the model.

Specified by:
addChangeListener in interface ButtonModel
Parameters:
l - The listener to add
See Also:
ButtonModel.removeChangeListener(ChangeListener)

removeChangeListener

public void removeChangeListener(ChangeListener l)
Remove a ChangeListener to the model. Usually only called to unsubscribe an AbstractButton's listener to the model.

Specified by:
removeChangeListener in interface ButtonModel
Parameters:
l - The listener to remove
See Also:
ButtonModel.removeChangeListener(ChangeListener)

getChangeListeners

public ChangeListener[] getChangeListeners()
Returns all registered ChangeListener objects.

Returns:
array of ChangeListener objects

fireItemStateChanged

protected void fireItemStateChanged(ItemEvent e)
Inform each ItemListener in the listenerList that an ItemEvent has occurred. This happens in response to any change to the stateMask field.

Parameters:
e - The ItemEvent to fire

fireActionPerformed

protected void fireActionPerformed(ActionEvent e)
Inform each ActionListener in the listenerList that an ActionEvent has occurred. This happens in response to the any change to the stateMask field which makes the enabled, armed and pressed properties all simultaneously true.

Parameters:
e - The ActionEvent to fire

fireStateChanged

protected void fireStateChanged()
Inform each ChangeListener in the listenerList that a ChangeEvent has occurred. This happens in response to the any change to a property of the model.


isArmed

public boolean isArmed()
Get the value of the model's "armed" property.

Specified by:
isArmed in interface ButtonModel
Returns:
The current "armed" property
See Also:
ButtonModel.setArmed(boolean)

setArmed

public void setArmed(boolean a)
Set the value of the model's "armed" property.

Specified by:
setArmed in interface ButtonModel
Parameters:
a - The new "armed" property
See Also:
ButtonModel.isArmed()

isEnabled

public boolean isEnabled()
Get the value of the model's "enabled" property.

Specified by:
isEnabled in interface ButtonModel
Returns:
The current "enabled" property.
See Also:
ButtonModel.setEnabled(boolean)

setEnabled

public void setEnabled(boolean e)
Set the value of the model's "enabled" property.

Specified by:
setEnabled in interface ButtonModel
Parameters:
e - The new "enabled" property
See Also:
ButtonModel.isEnabled()

setPressed

public void setPressed(boolean p)
Set the value of the model's "pressed" property.

Specified by:
setPressed in interface ButtonModel
Parameters:
p - The new "pressed" property
See Also:
ButtonModel.isPressed()

isPressed

public boolean isPressed()
Get the value of the model's "pressed" property.

Specified by:
isPressed in interface ButtonModel
Returns:
The current "pressed" property
See Also:
ButtonModel.setPressed(boolean)

setRollover

public void setRollover(boolean r)
Set the value of the model's "rollover" property.

Specified by:
setRollover in interface ButtonModel
Parameters:
r - The new "rollover" property
See Also:
ButtonModel.isRollover()

setSelected

public void setSelected(boolean s)
Set the value of the model's "selected" property.

Specified by:
setSelected in interface ButtonModel
Parameters:
s - The new "selected" property
See Also:
ButtonModel.isSelected()

isSelected

public boolean isSelected()
Get the value of the model's "selected" property.

Specified by:
isSelected in interface ButtonModel
Returns:
The current "selected" property
See Also:
ButtonModel.setSelected(boolean)

isRollover

public boolean isRollover()
Get the value of the model's "rollover" property.

Specified by:
isRollover in interface ButtonModel
Returns:
The current "rollover" property
See Also:
ButtonModel.setRollover(boolean)

getMnemonic

public int getMnemonic()
Get the value of the model's "mnemonic" property.

Specified by:
getMnemonic in interface ButtonModel
Returns:
The current "mnemonic" property
See Also:
ButtonModel.setMnemonic(int)

setMnemonic

public void setMnemonic(int key)
Set the value of the model's "mnemonic" property.

Specified by:
setMnemonic in interface ButtonModel
Parameters:
key - The new "mnemonic" property
See Also:
ButtonModel.getMnemonic()

setActionCommand

public void setActionCommand(String s)
Set the value of the model's "actionCommand" property. This property is used as the "command" property of the ActionEvent fired from the model.

Specified by:
setActionCommand in interface ButtonModel
Parameters:
s - The new "actionCommand" property.
See Also:
ButtonModel.getActionCommand()

getActionCommand

public String getActionCommand()
Returns the current value of the model's "actionCommand" property.

Specified by:
getActionCommand in interface ButtonModel
Returns:
The current "actionCommand" property
See Also:
ButtonModel.setActionCommand(String)

setGroup

public void setGroup(ButtonGroup g)
Set the value of the model's "group" property. The model is said to be a member of the ButtonGroup held in its "group" property, and only one model in a given group can have their "selected" property be true at a time.

Specified by:
setGroup in interface ButtonModel
Parameters:
g - The new "group" property (null permitted).
See Also:
getGroup()

getGroup

public ButtonGroup getGroup()
Returns the current value of the model's "group" property.

Returns:
The value of the "group" property
See Also:
setGroup(ButtonGroup)