APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing

public Class ButtonGroup

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.awt.event.*, java.util.Vector, .Enumeration, java.io.Serializable

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning "on" one of those buttons turns off all other buttons in the group.

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

Initially, all buttons in the group are unselected.

For examples and further information on using button groups see How to Use Radio Buttons, a section in The Java Tutorial.

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Jeff Dinkins
Since
1.2

Field Summary

Modifier and TypeField and Description
protected Vector<AbstractButton>
buttons

The list of buttons participating in this group.

pack-priv ButtonModel
selection

The current selection.

Constructor Summary

AccessConstructor and Description
public
ButtonGroup()

Creates a new ButtonGroup.

Method Summary

Modifier and TypeMethod and Description
public void
add(AbstractButton
the button to be added
b
)

Adds the button to the group.

public void
clearSelection()

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

public int

Returns:

the button count
getButtonCount
()

Returns the number of buttons in the group.

public Enumeration<AbstractButton>

Returns:

an Enumeration of the buttons in this group
getElements
()

Returns all the buttons that are participating in this group.

public ButtonModel

Returns:

the selected button model
getSelection
()

Returns the model of the selected button.

public boolean

Returns:

true if the button is selected, otherwise returns false
isSelected
(ButtonModel
an instance of ButtonModel
m
)

Returns whether a ButtonModel is selected.

public void
remove(AbstractButton
the button to be removed
b
)

Removes the button from the group.

public void
setSelected(ButtonModel
the ButtonModel
m
,
boolean
true if this button is to be selected, otherwise false
b
)

Sets the selected value for the ButtonModel.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

buttonsback to summary
protected Vector<AbstractButton> buttons

The list of buttons participating in this group.

selectionback to summary
pack-priv ButtonModel selection

The current selection.

Constructor Detail

ButtonGroupback to summary
public ButtonGroup()

Creates a new ButtonGroup.

Method Detail

addback to summary
public void add(AbstractButton b)

Adds the button to the group.

Parameters
b:AbstractButton

the button to be added

clearSelectionback to summary
public void clearSelection()

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

Since
1.6
getButtonCountback to summary
public int getButtonCount()

Returns the number of buttons in the group.

Returns:int

the button count

Since
1.3
getElementsback to summary
public Enumeration<AbstractButton> getElements()

Returns all the buttons that are participating in this group.

Returns:Enumeration<AbstractButton>

an Enumeration of the buttons in this group

getSelectionback to summary
public ButtonModel getSelection()

Returns the model of the selected button.

Returns:ButtonModel

the selected button model

isSelectedback to summary
public boolean isSelected(ButtonModel m)

Returns whether a ButtonModel is selected.

Parameters
m:ButtonModel

an instance of ButtonModel

Returns:boolean

true if the button is selected, otherwise returns false

removeback to summary
public void remove(AbstractButton b)

Removes the button from the group.

Parameters
b:AbstractButton

the button to be removed

setSelectedback to summary
public void setSelected(ButtonModel m, boolean b)

Sets the selected value for the ButtonModel. Only one button in the group may be selected at a time.

Parameters
m:ButtonModel

the ButtonModel

b:boolean

true if this button is to be selected, otherwise false