5.2 The DefaultButtonModel Class


Swing provides an implementation of the ButtonModel interface called DefaultButtonModel. This class is used directly by the AbstractButton class and indirectly by the other button classes. JToggleButton defines an inner class extending DefaultButtonModel that it and its descendants use to manage their state data.

5.2.1 Properties

The DefaultButtonModel class gets most of its properties from ButtonModel. The default values set by this class are shown in Table 5-3.

Table 5-3. DefaultButtonModel properties

Property

Data type

get

is

set

Default value

actionCommando

String

·

 

·

null

armedo

boolean

 

·

·

false

enabledo

boolean

 

·

·

true

groupo, *

ButtonGroup

·

 

·

null

mnemonico

int

·

 

·

KeyEvent.VK_UNDEFINED

pressedo

boolean

 

·

·

false

rollovero

boolean

 

·

·

false

selectedo

boolean

 

·

·

false

selectedObjects

Object[]

·

   

null

ooverridden, *getter was introduced in SDK 1.3

The only property here that does not come from the ButtonModel interface is the selectedObjects property. DefaultButtonModel provides a getSelectedObjects( ) method because it is mandated by the ItemSelectable interface, but it always returns null. SDK 1.3 added the getGroup( ) method.

5.2.2 Events

The events fired by DefaultButtonModel are those required by ButtonModel and listed in Table 5-2. An ActionEvent is fired when the button is pressed, an ItemEvent is fired when the button's state is changed, and a ChangeEvent is fired when a change has occurred to the button's properties.

This class implements the following standard methods:

public void addActionListener(ActionListener l)
public void removeActionListener(ActionListener l)
public ActionListener[] getActionListeners( ) (added in SDK 1.3)
public void addItemListener(ItemListener l)
public void removeItemListener(ItemListener l)
public ItemListener[] getItemListeners( ) (added in SDK 1.3)
public void addChangeListener(ChangeListener l)
public void removeChangeListener(ChangeListener l)
public ChangeListener[] getChangeListeners( ) (added in SDK 1.3)
public EventListener[] getListeners(Class listenerType) (added in SDK 1.4)

Note that the ButtonModel properties are not technically "bound properties" as defined by the JavaBeans specification because the lighter-weight ChangeEvent is sent when they change, rather than the standard PropertyChangeEvent.

5.2.3 Constants

DefaultButtonModel uses the constants shown in Table 5-4 to store internal state.

Table 5-4. DefaultButtonModel constants

Constant

Type

ARMED

int

ENABLED

int

PRESSED

int

ROLLOVER

int

SELECTED

int

5.2.4 Constructor

public DefaultButtonModel( )

Instantiates a new model. The model's properties are shown in Table 5-3.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net