Java Reference
In-Depth Information
Figure 5-10.
Comparing JRadioButton to JCheckBox appearance
The
JRadioButton
is made up of several pieces. Like
JToggleButton
and
JCheckBox
, the
JRadioButton
uses a
ToggleButtonModel
to represent its data model. It uses a
ButtonGroup
through
AbstractButton
to provide the mutually exclusive grouping, and the user interface
delegate is the
RadioButtonUI
.
Let's now explore how to use the different pieces of a
JRadioButton
.
Creating JRadioButton Components
As with
JCheckBox
and
JToggleButton
, there are eight constructors for
JRadioButton
:
public JRadioButton()
JRadioButton aRadioButton = new JRadioButton();
public JRadioButton(Icon icon)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));
public JRadioButton(Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false),
true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));
public JRadioButton(String text)
JRadioButton aRadioButton = new JRadioButton("4 slices");
public JRadioButton(String text, boolean selected)
JRadioButton aRadioButton = new JRadioButton("8 slices", true);
public JRadioButton(String text, Icon icon)
JRadioButton aRadioButton = new JRadioButton("12 slices",
new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));
public JRadioButton(String text, Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton("16 slices",
new DiamondIcon(Color.CYAN, false), true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));