DefaultButtonModel (Java 2 Platform SE 5.0) (original) (raw)


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


Field Summary
protected String actionCommand
static int ARMED Indicates partial commitment towards choosing the button.
protected ChangeEvent changeEvent Only one ChangeEvent is needed per button model instance since the event's only state is the source property.
static int ENABLED Indicates that the button can be selected by an input device (such as a mouse pointer).
protected ButtonGroup group
protected EventListenerList listenerList
protected int mnemonic
static int PRESSED Indicates that the button has been "pressed" (typically, when the mouse is released).
static int ROLLOVER Indicates that the mouse is over the button.
static int SELECTED Indicates that the button has been selected.
protected int stateMask
Constructor Summary
DefaultButtonModel() Constructs a default JButtonModel.
Method Summary
void addActionListener(ActionListener l) Adds an ActionListener to the button.
void addChangeListener(ChangeListener l) Adds a ChangeListener to the button.
void addItemListener(ItemListener l) Adds an ItemListener to the button.
protected void fireActionPerformed(ActionEvent e) Notifies all listeners that have registered interest for notification on this event type.
protected void fireItemStateChanged(ItemEvent e) Notifies all listeners that have registered interest for notification on this event type.
protected void fireStateChanged() Notifies all listeners that have registered interest for notification on this event type.
String getActionCommand() Returns the action command for this 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 this button belongs to.
ItemListener[] getItemListeners() Returns an array of all the item listeners registered on this DefaultButtonModel.
<T extends EventListener> T[] getListeners(Class listenerType) Returns an array of all the objects currently registered as_Foo_Listeners upon this model.
int getMnemonic() Gets the keyboard mnemonic for this model
Object[] getSelectedObjects() Overridden to return null.
boolean isArmed() Indicates partial commitment towards pressing the button.
boolean isEnabled() Indicates whether the button can be selected or pressed by an input device (such as a mouse pointer).
boolean isPressed() Indicates whether button has been 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 button.
void removeChangeListener(ChangeListener l) Removes a ChangeListener from the button.
void removeItemListener(ItemListener l) Removes an ItemListener from the button.
void setActionCommand(String actionCommand) Sets the actionCommand string that gets sent as part of the event when the button is pressed.
void setArmed(boolean b) Marks the button as "armed".
void setEnabled(boolean b) Enables or disables the button.
void setGroup(ButtonGroup group) Identifies the group this 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 this 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.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29)
Field Detail

stateMask

protected int stateMask


actionCommand

protected String actionCommand


group

protected ButtonGroup group


mnemonic

protected int mnemonic


changeEvent

protected transient 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".


listenerList

protected EventListenerList listenerList


ARMED

public static final int ARMED

Indicates partial commitment towards choosing the button.

See Also:

Constant Field Values


SELECTED

public static final int SELECTED

Indicates that the button has been selected. Only needed for certain types of buttons - such as RadioButton or Checkbox.

See Also:

Constant Field Values


PRESSED

public static final int PRESSED

Indicates that the button has been "pressed" (typically, when the mouse is released).

See Also:

Constant Field Values


ENABLED

public static final int ENABLED

Indicates that the button can be selected by an input device (such as a mouse pointer).

See Also:

Constant Field Values


ROLLOVER

public static final int ROLLOVER

Indicates that the mouse is over the button.

See Also:

Constant Field Values

Constructor Detail

DefaultButtonModel

public DefaultButtonModel()

Constructs a default JButtonModel.

Method Detail

setActionCommand

public void setActionCommand(String actionCommand)

Sets the actionCommand string that gets sent as part of the event when the button is pressed.

Specified by:

[setActionCommand](../../javax/swing/ButtonModel.html#setActionCommand%28java.lang.String%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

actionCommand - the String that identifies the generated event


getActionCommand

public String getActionCommand()

Returns the action command for this button.

Specified by:

[getActionCommand](../../javax/swing/ButtonModel.html#getActionCommand%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

the String that identifies the generated event

See Also:

setActionCommand(java.lang.String)


isArmed

public boolean isArmed()

Indicates partial commitment towards pressing the button.

Specified by:

[isArmed](../../javax/swing/ButtonModel.html#isArmed%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

true if the button is armed, and ready to be pressed

See Also:

setArmed(boolean)


isSelected

public boolean isSelected()

Indicates if the button has been selected. Only needed for certain types of buttons - such as RadioButton or Checkbox.

Specified by:

[isSelected](../../javax/swing/ButtonModel.html#isSelected%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

true if the button is selected


isEnabled

public boolean isEnabled()

Indicates whether the button can be selected or pressed by an input device (such as a mouse pointer). (Checkbox-buttons are selected, regular buttons are "pressed".)

Specified by:

[isEnabled](../../javax/swing/ButtonModel.html#isEnabled%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

true if the button is enabled, and therefore selectable (or pressable)


isPressed

public boolean isPressed()

Indicates whether button has been pressed.

Specified by:

[isPressed](../../javax/swing/ButtonModel.html#isPressed%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

true if the button has been pressed


isRollover

public boolean isRollover()

Indicates that the mouse is over the button.

Specified by:

[isRollover](../../javax/swing/ButtonModel.html#isRollover%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

true if the mouse is over the button


setArmed

public void setArmed(boolean b)

Marks the button as "armed". If the mouse button is released while it is over this item, the button's action event fires. If the mouse button is released elsewhere, the event does not fire and the button is disarmed.

Specified by:

[setArmed](../../javax/swing/ButtonModel.html#setArmed%28boolean%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

b - true to arm the button so it can be selected


setEnabled

public void setEnabled(boolean b)

Enables or disables the button.

Specified by:

[setEnabled](../../javax/swing/ButtonModel.html#setEnabled%28boolean%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

b - true to enable the button

See Also:

isEnabled()


setSelected

public void setSelected(boolean b)

Selects or deselects the button.

Specified by:

[setSelected](../../javax/swing/ButtonModel.html#setSelected%28boolean%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

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](../../javax/swing/ButtonModel.html#setPressed%28boolean%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

b - true to set the button to "pressed"

See Also:

isPressed()


setRollover

public void setRollover(boolean b)

Sets or clears the button's rollover state

Specified by:

[setRollover](../../javax/swing/ButtonModel.html#setRollover%28boolean%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

b - true to turn on rollover

See Also:

isRollover()


setMnemonic

public void setMnemonic(int key)

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

Specified by:

[setMnemonic](../../javax/swing/ButtonModel.html#setMnemonic%28int%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

key - an int specifying the accelerator key


getMnemonic

public int getMnemonic()

Gets the keyboard mnemonic for this model

Specified by:

[getMnemonic](../../javax/swing/ButtonModel.html#getMnemonic%28%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Returns:

an int specifying the accelerator key

See Also:

setMnemonic(int)


addChangeListener

public void addChangeListener(ChangeListener l)

Adds a ChangeListener to the button.

Specified by:

[addChangeListener](../../javax/swing/ButtonModel.html#addChangeListener%28javax.swing.event.ChangeListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to add


removeChangeListener

public void removeChangeListener(ChangeListener l)

Removes a ChangeListener from the button.

Specified by:

[removeChangeListener](../../javax/swing/ButtonModel.html#removeChangeListener%28javax.swing.event.ChangeListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to remove


getChangeListeners

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

Since:

1.4

See Also:

addChangeListener(javax.swing.event.ChangeListener), removeChangeListener(javax.swing.event.ChangeListener)


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:

EventListenerList


addActionListener

public void addActionListener(ActionListener l)

Adds an ActionListener to the button.

Specified by:

[addActionListener](../../javax/swing/ButtonModel.html#addActionListener%28java.awt.event.ActionListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to add


removeActionListener

public void removeActionListener(ActionListener l)

Removes an ActionListener from the button.

Specified by:

[removeActionListener](../../javax/swing/ButtonModel.html#removeActionListener%28java.awt.event.ActionListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to remove


getActionListeners

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

Since:

1.4

See Also:

addActionListener(java.awt.event.ActionListener), removeActionListener(java.awt.event.ActionListener)


fireActionPerformed

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

See Also:

EventListenerList


addItemListener

public void addItemListener(ItemListener l)

Adds an ItemListener to the button.

Specified by:

[addItemListener](../../java/awt/ItemSelectable.html#addItemListener%28java.awt.event.ItemListener%29) in interface [ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")

Specified by:

[addItemListener](../../javax/swing/ButtonModel.html#addItemListener%28java.awt.event.ItemListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to add

See Also:

ItemEvent


removeItemListener

public void removeItemListener(ItemListener l)

Removes an ItemListener from the button.

Specified by:

[removeItemListener](../../java/awt/ItemSelectable.html#removeItemListener%28java.awt.event.ItemListener%29) in interface [ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")

Specified by:

[removeItemListener](../../javax/swing/ButtonModel.html#removeItemListener%28java.awt.event.ItemListener%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

l - the listener to remove

See Also:

ItemEvent


getItemListeners

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

Since:

1.4

See Also:

addItemListener(java.awt.event.ItemListener), removeItemListener(java.awt.event.ItemListener)


fireItemStateChanged

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

See Also:

EventListenerList


getListeners

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

Returns an array of all the objects currently registered as_Foo_Listeners upon this model._Foo_Listeners are registered using the add_Foo_Listener method.

You can specify the listenerType argument with a class literal, such as _Foo_Listener.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_Foo_Listeners on this model, or an empty array if no such listeners have been added

Throws:

[ClassCastException](../../java/lang/ClassCastException.html "class in java.lang") - if listenerType doesn't specify a class or interface that implementsjava.util.EventListener

Since:

1.3

See Also:

getActionListeners(), getChangeListeners(), getItemListeners()


getSelectedObjects

public Object[] getSelectedObjects()

Overridden to return null.

Specified by:

[getSelectedObjects](../../java/awt/ItemSelectable.html#getSelectedObjects%28%29) in interface [ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")


setGroup

public void setGroup(ButtonGroup group)

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

Specified by:

[setGroup](../../javax/swing/ButtonModel.html#setGroup%28javax.swing.ButtonGroup%29) in interface [ButtonModel](../../javax/swing/ButtonModel.html "interface in javax.swing")

Parameters:

group - the ButtonGroup this button belongs to


getGroup

public ButtonGroup getGroup()

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

Returns:

a ButtonGroup that this button belongs to

Since:

1.3



Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.