Java Reference
In-Depth Information
public JRadioButton(Action action)
Action action = ...;
JRadioButton aRadioButton = new JRadioButton(action);
Note As with a JCheckBox , configuring a JRadioButton from an Action sets the label, state, and
tooltip text, but not the icon.
Each allows you to customize one or more of the label, icon, or initial selection state prop-
erties. Unless specified otherwise, there's no text in the label, and the default selected/unselected
icon for the check box appears unselected. After creating a group of radio button components,
you need to place each into a single ButtonGroup so that they work as expected, with only
one button in the group selectable at a time. If you do initialize the icon in the constructor,
it's the icon for the unselected state of the check box, with the same icon displayed when
the check box is selected. You must also either initialize the selected icon with the
setSelectedIcon(Icon newValue) method, described with JCheckBox , or make sure the icon
is state-aware and updates itself.
JRadioButton Properties
JRadioButton has two properties that override the behavior of its parent JToggleButton , as
listed in Table 5-5.
Table 5-5. JRadioButton Properties
Property Name
Data Type
Access
accessibleContext
AccessibleContext
Read-only
UIClassID
String
Read-only
Grouping JRadioButton Components in a ButtonGroup
The JRadioButton is the only JToggleButton subclass that should be placed in a ButtonGroup
in order to work properly. Merely creating a bunch of radio buttons and placing them on the
screen isn't enough to make them behave appropriately. In addition to adding each radio
button to a container, you need to create a ButtonGroup and add each radio button to the same
ButtonGroup . Once all the JRadioButton items are in a group, whenever an unselected radio
button is selected, the ButtonGroup causes the currently selected radio button to be deselected.
Placing a set of JRadioButton components within a ButtonGroup on the screen is basically
a four-step process:
1.
Create a container for the group.
JPanel aPanel = new JPanel(new GridLayout(0, 1));
 
Search WWH ::




Custom Search