Java Reference
In-Depth Information
if (title != null) {
Border border = BorderFactory.createTitledBorder(title);
panel.setBorder(border);
}
// Create group
ButtonGroup group = new ButtonGroup();
JRadioButton aRadioButton;
// For each String passed in:
// Create button, add to panel, and add to group
for (int i=0, n=elements.length; i<n; i++) {
aRadioButton = new JRadioButton (elements[i]);
panel.add(aRadioButton);
group.add(aRadioButton);
if (actionListener != null) {
aRadioButton.addActionListener(actionListener);
}
if (itemListener != null) {
aRadioButton.addItemListener(itemListener);
}
if (changeListener != null) {
aRadioButton.addChangeListener(changeListener);
}
}
return panel;
}
}
■
Note
One thing not shown here but explained in Chapter 4 in the discussion of
ButtonModel
and
DefaultButtonModel
is how to get the
ButtonGroup
when given a
JRadioButton
. If you want to find
the
ButtonGroup
that a
JRadioButton
is in, you need to ask the
DefaultButtonModel
:
ButtonGroup
group = ((DefaultButtonModel)aJRadioButton.getModel()).getButtonGroup()
.
Customizing a JRadioButton Look and Feel
Each installable Swing look and feel provides a different
JRadioButton
appearance and set of
default
UIResource
values. Figure 5-13 shows the appearance of the
JRadioButton
component
for the preinstalled set of look and feel types: Motif, Windows, and Ocean. All three screens
show 4 slices of Thin Crust pizza as the order. In addition, the Thick Crust option has the input
focus.