javax.swing
Class ButtonGroup

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.swing.ButtonGroup
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public class ButtonGroup
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Serializable sample code for java.io.Serializable definition code for 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. Once any button is selected, one button is always selected in the group. There is no way to turn a button programmatically to "off", in order to clear the button group. To give the appearance of "none selected", add an invisible radio button to the group and then programmatically select that button to turn off all the displayed radio buttons. For example, a normal button with the label "none" could be wired to select the invisible radio button.

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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder sample code for java.beans.XMLEncoder definition code for java.beans.XMLEncoder .

See Also:
Serialized Form

Field Summary
protected  Vector sample code for java.util.Vector definition code for java.util.Vector <AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton > buttons sample code for javax.swing.ButtonGroup.buttons definition code for javax.swing.ButtonGroup.buttons
           
 
Constructor Summary
ButtonGroup sample code for javax.swing.ButtonGroup.ButtonGroup() definition code for javax.swing.ButtonGroup.ButtonGroup() ()
          Creates a new ButtonGroup.
 
Method Summary
 void add sample code for javax.swing.ButtonGroup.add(javax.swing.AbstractButton) definition code for javax.swing.ButtonGroup.add(javax.swing.AbstractButton) (AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton  b)
          Adds the button to the group.
 int getButtonCount sample code for javax.swing.ButtonGroup.getButtonCount() definition code for javax.swing.ButtonGroup.getButtonCount() ()
          Returns the number of buttons in the group.
 Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton > getElements sample code for javax.swing.ButtonGroup.getElements() definition code for javax.swing.ButtonGroup.getElements() ()
          Returns all the buttons that are participating in this group.
 ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.ButtonModel getSelection sample code for javax.swing.ButtonGroup.getSelection() definition code for javax.swing.ButtonGroup.getSelection() ()
          Returns the model of the selected button.
 boolean isSelected sample code for javax.swing.ButtonGroup.isSelected(javax.swing.ButtonModel) definition code for javax.swing.ButtonGroup.isSelected(javax.swing.ButtonModel) (ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.ButtonModel  m)
          Returns whether a ButtonModel is selected.
 void remove sample code for javax.swing.ButtonGroup.remove(javax.swing.AbstractButton) definition code for javax.swing.ButtonGroup.remove(javax.swing.AbstractButton) (AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton  b)
          Removes the button from the group.
 void setSelected sample code for javax.swing.ButtonGroup.setSelected(javax.swing.ButtonModel, boolean) definition code for javax.swing.ButtonGroup.setSelected(javax.swing.ButtonModel, boolean) (ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.ButtonModel  m, boolean b)
          Sets the selected value for the ButtonModel.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

buttons sample code for javax.swing.ButtonGroup.buttons

protected Vector sample code for java.util.Vector definition code for java.util.Vector <AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton > buttons
Constructor Detail

ButtonGroup sample code for javax.swing.ButtonGroup() definition code for javax.swing.ButtonGroup()

public ButtonGroup()
Creates a new ButtonGroup.

Method Detail

add sample code for javax.swing.ButtonGroup.add(javax.swing.AbstractButton) definition code for javax.swing.ButtonGroup.add(javax.swing.AbstractButton)

public void add(AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton  b)
Adds the button to the group.

Parameters:
b - the button to be added

remove sample code for javax.swing.ButtonGroup.remove(javax.swing.AbstractButton) definition code for javax.swing.ButtonGroup.remove(javax.swing.AbstractButton)

public void remove(AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton  b)
Removes the button from the group.

Parameters:
b - the button to be removed

getElements sample code for javax.swing.ButtonGroup.getElements() definition code for javax.swing.ButtonGroup.getElements()

public Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <AbstractButton sample code for javax.swing.AbstractButton definition code for javax.swing.AbstractButton > getElements()
Returns all the buttons that are participating in this group.

Returns:
an Enumeration of the buttons in this group

getSelection sample code for javax.swing.ButtonGroup.getSelection() definition code for javax.swing.ButtonGroup.getSelection()

public ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.ButtonModel  getSelection()
Returns the model of the selected button.

Returns:
the selected button model

setSelected sample code for javax.swing.ButtonGroup.setSelected(javax.swing.ButtonModel, boolean) definition code for javax.swing.ButtonGroup.setSelected(javax.swing.ButtonModel, boolean)

public void setSelected(ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.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 - the ButtonModel
b - true if this button is to be selected, otherwise false

isSelected sample code for javax.swing.ButtonGroup.isSelected(javax.swing.ButtonModel) definition code for javax.swing.ButtonGroup.isSelected(javax.swing.ButtonModel)

public boolean isSelected(ButtonModel sample code for javax.swing.ButtonModel definition code for javax.swing.ButtonModel  m)
Returns whether a ButtonModel is selected.

Returns:
true if the button is selected, otherwise returns false

getButtonCount sample code for javax.swing.ButtonGroup.getButtonCount() definition code for javax.swing.ButtonGroup.getButtonCount()

public int getButtonCount()
Returns the number of buttons in the group.

Returns:
the button count
Since:
1.3