javax.swing

Class DefaultButtonModel

public class DefaultButtonModel extends Object implements ButtonModel, Serializable

The default implementation of {@link 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.
Field Summary
static intARMED
Indicates that the button is partially committed to being pressed, but not entirely.
protected StringactionCommand
The string used as the "command" property of any ActionEvent this model sends.
protected ChangeEventchangeEvent
The single ChangeEvent this model (re)uses to call its ChangeListeners.
static intENABLED
State constant indicating that the button is enabled.
protected ButtonGroupgroup
The group this model belongs to.
protected EventListenerListlistenerList
List of ItemListeners, ChangeListeners, and ActionListeners registered on this model.
protected intmnemonic
The key code (one of {@link java.awt.event.KeyEvent} VK_) used to press this button via a keyboard interface.
static intPRESSED
State constant indicating that the user is holding down the button.
static intROLLOVER
State constant indicating that the mouse is currently positioned over the button.
protected intstateMask
Represents the "state properties" (armed, enabled, pressed, rollover and selected) by a bitwise combination of integer constants.
static intSELECTED
State constant indicating that the button is selected.
Constructor Summary
DefaultButtonModel()
Creates a new DefaultButtonModel object.
Method Summary
voidaddActionListener(ActionListener l)
Add an ActionListener to the model.
voidaddChangeListener(ChangeListener l)
Add a ChangeListener to the model.
voidaddItemListener(ItemListener l)
Add an ItemListener to the model.
protected voidfireActionPerformed(ActionEvent e)
Inform each ActionListener in the {@link #listenerList} that an ActionEvent has occurred.
protected voidfireItemStateChanged(ItemEvent e)
Inform each ItemListener in the {@link #listenerList} that an ItemEvent has occurred.
protected voidfireStateChanged()
Inform each ChangeListener in the {@link #listenerList} that a ChangeEvent has occurred.
StringgetActionCommand()
Returns the current value of the model's "actionCommand" property.
ActionListener[]getActionListeners()
Returns all registered ActionListener objects.
ChangeListener[]getChangeListeners()
Returns all registered ChangeListener objects.
ButtonGroupgetGroup()
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.
intgetMnemonic()
Get the value of the model's "mnemonic" property.
Object[]getSelectedObjects()
Return null.
booleanisArmed()
Get the value of the model's "armed" property.
booleanisEnabled()
Get the value of the model's "enabled" property.
booleanisPressed()
Get the value of the model's "pressed" property.
booleanisRollover()
Get the value of the model's "rollover" property.
booleanisSelected()
Get the value of the model's "selected" property.
voidremoveActionListener(ActionListener l)
Remove an ActionListener to the model.
voidremoveChangeListener(ChangeListener l)
Remove a ChangeListener to the model.
voidremoveItemListener(ItemListener l)
Remove an ItemListener to the model.
voidsetActionCommand(String s)
Set the value of the model's "actionCommand" property.
voidsetArmed(boolean a)
Set the value of the model's "armed" property.
voidsetEnabled(boolean e)
Set the value of the model's "enabled" property.
voidsetGroup(ButtonGroup g)
Set the value of the model's "group" property.
voidsetMnemonic(int key)
Set the value of the model's "mnemonic" property.
voidsetPressed(boolean p)
Set the value of the model's "pressed" property.
voidsetRollover(boolean r)
Set the value of the model's "rollover" property.
voidsetSelected(boolean s)
Set the value of the model's "selected" property.

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.

actionCommand

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

changeEvent

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

ENABLED

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

group

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

listenerList

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

mnemonic

protected int mnemonic
The key code (one of {@link java.awt.event.KeyEvent} VK_) used to press this button via a keyboard interface.

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.

ROLLOVER

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

stateMask

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

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

Constructor Detail

DefaultButtonModel

public DefaultButtonModel()
Creates a new DefaultButtonModel object.

Method Detail

addActionListener

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

Parameters: l The listener to add

addChangeListener

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

Parameters: l The listener to add

addItemListener

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

Parameters: l The listener to add

fireActionPerformed

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

Parameters: e The ActionEvent to fire

fireItemStateChanged

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

Parameters: e The ItemEvent to fire

fireStateChanged

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

getActionCommand

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

Returns: The current "actionCommand" property

getActionListeners

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

Returns: array of ActionListener objects

getChangeListeners

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

Returns: array of ChangeListener objects

getGroup

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

Returns: The value of the "group" property

See Also: setGroup

getItemListeners

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

Returns: array of ItemListener objects

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

getMnemonic

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

Returns: The current "mnemonic" property

getSelectedObjects

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

Returns: null

isArmed

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

Returns: The current "armed" property

isEnabled

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

Returns: The current "enabled" property.

isPressed

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

Returns: The current "pressed" property

isRollover

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

Returns: The current "rollover" property

isSelected

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

Returns: The current "selected" property

removeActionListener

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

Parameters: l The listener to remove

removeChangeListener

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

Parameters: l The listener to remove

removeItemListener

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

Parameters: l The listener to remove

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 {@link ActionEvent} fired from the model.

Parameters: s The new "actionCommand" property.

setArmed

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

Parameters: a The new "armed" property

setEnabled

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

Parameters: e The new "enabled" property

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 {@link ButtonGroup} held in its "group" property, and only one model in a given group can have their "selected" property be true at a time.

Parameters: g The new "group" property (null permitted).

See Also: getGroup

setMnemonic

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

Parameters: key The new "mnemonic" property

setPressed

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

Parameters: p The new "pressed" property

setRollover

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

Parameters: r The new "rollover" property

setSelected

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

Parameters: s The new "selected" property