Groovy Documentation

com.wordpress.tipsforjava.swing
[Java] Class SelectButtonGroup

java.lang.Object
  javax.swing.ButtonGroup
      com.wordpress.tipsforjava.swing.SelectButtonGroup
All Implemented Interfaces:
Serializable

public class SelectButtonGroup
extends ButtonGroup

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same SelectButtonGroup object means that selecting one of those buttons deselects all other buttons in the group.

This class provides useful methods not available in javax.swing.ButtonGroup to simultaneously enable or disable all the buttons in the group, to listen for a change of selected button by adding a PropertyChangeListener, and to obtain the selected index as an int, facilitating switch-case processing.

A SelectButtonGroup can be used with any set of objects that inherit from AbstractButton. Typically a button group contains instances of JRadioButton, JRadioButtonMenuItem, or JToggleButton. It wouldn't make sense to put an instance of JButton or JMenuItem in a button group because JButton and JMenuItem don't implement the selected state.

When a SelectButtonGroup is no longer required, #dispose() should be invoked to release static references to the buttons it contains.

Extends ButtonGroup.

Authors:
Rob Camick
Darryl Burke
See Also:
dispose


Field Summary
static Vector allButtons

The collection of all buttons present in SelectedButtonGroups

protected boolean enabled

The enabled state of the buttons in the group

private SwingPropertyChangeSupport propertyChangeSupport

all propertyChangeSupport goes through this.

AbstractButton selectedButton

The currently selected button.

 
Constructor Summary
SelectButtonGroup()

Creates a new SelectButtonGroup.

 
Method Summary
void add(AbstractButton button)

Adds the button to the group.

void addPropertyChangeListener(PropertyChangeListener listener)

Adds a PropertyChangeListener to the listener list.

void clearSelection()

Clears the selection such that none of the buttons in the SelectButtonGroup are selected.

void dispose()

Removes all the buttons from the group

void finalize()

Ensures that the dispose method of this group is called when there are no more refrences to it.

protected void firePropertyChanged(String propertyName, Object oldValue, Object newValue)

Support for reporting bound property changes for Object properties.

PropertyChangeListener[] getPropertyChangeListeners()

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

AbstractButton getSelectedButton()

Gets the current selected button from this group.

int getSelectedIndex()

Returns the index of the selected button.

boolean isSelected(AbstractButton button)

Returns whether a Button is selected.

void remove(AbstractButton button)

Removes the button from the group.

void removePropertyChangeListener(PropertyChangeListener listener)

Removes a PropertyChangeListener from the listener list.

void setEnabled(boolean enable)

Enables or disables all the buttons in this group, depending on the value of the parameter enable.

void setSelected(ButtonModel model, boolean selected)

Sets the selected value for the ButtonModel.

void setSelectedButton(AbstractButton button)

Sets the selected value for the AbstractButton.

void setSelectedIndex(int index)

Sets the group's selected index to index.

 
Methods inherited from class ButtonGroup
add, remove, getElements, isSelected, setSelected, clearSelection, getSelection, getButtonCount, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

allButtons

static Vector allButtons
The collection of all buttons present in SelectedButtonGroups


enabled

protected boolean enabled
The enabled state of the buttons in the group


propertyChangeSupport

private SwingPropertyChangeSupport propertyChangeSupport
all propertyChangeSupport goes through this.


selectedButton

AbstractButton selectedButton
The currently selected button.


 
Constructor Detail

SelectButtonGroup

public SelectButtonGroup()
Creates a new SelectButtonGroup.


 
Method Detail

add

@Override
public void add(AbstractButton button)
Adds the button to the group.
Throws:
IllegalArgumentException if the button is already added to another group. A button whose model does not provide a method getGroup() may not be detected as belonging to another group.
Parameters:
button - the button to be added


addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. The listener is registered for all bound properties of this class, including the following:

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the property change listener to be added
See Also:
removePropertyChangeListener
getPropertyChangeListeners


clearSelection

@Override
public void clearSelection()
Clears the selection such that none of the buttons in the SelectButtonGroup are selected.
Since:
1.6


dispose

public void dispose()
Removes all the buttons from the group

It is recommended to invoke this method when the group is no longer required. This releases static references to the buttons of the group.


finalize

@Override
public void finalize()
Ensures that the dispose method of this group is called when there are no more refrences to it.
Throws:
Throwable if an error occurs.
See Also:
SelectButtonGroup.dispose


firePropertyChanged

protected void firePropertyChanged(String propertyName, Object oldValue, Object newValue)
Support for reporting bound property changes for Object properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
Parameters:
propertyName - the property whose value has changed
oldValue - the property's previous value
newValue - the property's new value


getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all the property change listeners registered on this group.
Returns:
all of this group's PropertyChangeListeners or an empty array if no property change listeners are currently registered
See Also:
addPropertyChangeListener
removePropertyChangeListener


getSelectedButton

public AbstractButton getSelectedButton()
Gets the current selected button from this group. The current selected button is the AbstractButton in this group that is currently in the selected state, or null if there is no currently selected button.
Returns:
the AbstractButton that is currently in the selected state, or null.
See Also:
AbstractButton
setSelectedButton


getSelectedIndex

public int getSelectedIndex()
Returns the index of the selected button.
Returns:
the the index of the selected button, or -1 if there is no selection.
See Also:
setSelectedIndex


isSelected

public boolean isSelected(AbstractButton button)
Returns whether a Button is selected.
Returns:
true if the button is selected, otherwise returns false


remove

@Override
public void remove(AbstractButton button)
Removes the button from the group.
Parameters:
button - the button to be removed


removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list. This method should be used to remove PropertyChangeListeners that were registered for all bound properties of this class.

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the PropertyChangeListener to be removed
See Also:
addPropertyChangeListener
getPropertyChangeListeners


setEnabled

public void setEnabled(boolean enable)
Enables or disables all the buttons in this group, depending on the value of the parameter enable. An enabled button can respond to user input and generate events.
Parameters:
enable


setSelected

@Override
public void setSelected(ButtonModel model, boolean selected)
Sets the selected value for the ButtonModel. Only one button in the group may be selected at a time.

Notifies any listeners if the model changes

Parameters:
model - the ButtonModel
selected - true if this button is to be selected, otherwise false
See Also:
getSelection
addPropertyChangeListener


setSelectedButton

public void setSelectedButton(AbstractButton button)
Sets the selected value for the AbstractButton. Only one button in the group may be selected at a time.

Notifies any listeners if the model changes

Parameters:
button - the AbstractButton
See Also:
getSelectedButton
addPropertyChangeListener


setSelectedIndex

public void setSelectedIndex(int index)
Sets the group's selected index to index. Only one button in the group may be selected at a time.

Notifies any listeners if the model changes

Parameters:
index - an int specifying the model selection
See Also:
getSelectedIndex
addPropertyChangeListener


 

Groovy Documentation