Java Reference
In-Depth Information
Add radio buttons into a ButtonGroup so that only one button in the group is
on at any time.
To create a set of radio buttons, first create each button individually, and then add
all buttons of the set to a ButtonGroup object:
JRadioButton smallButton = new
JRadioButton("Small");
JRadioButton mediumButton = new
JRadioButton("Medium");
JRadioButton largeButton = new
JRadioButton("Large");
ButtonGroup group = new ButtonGroup();
group.add(smallButton);
group.add(mediumButton);
group.add(largeButton);
Note that the button group does not place the buttons close to each other on the
container. The purpose of the button group is simply to find out which buttons to
turn off when one of them is turned on. It is still your job to arrange the buttons on
the screen.
The isSelected method is called to find out whether a button is currently
selected or not. For example,
if (largeButton.isSelected()) size = LARGE_SIZE;
Call setSelected(true) on one of the radio buttons in a radio button group
before making the enclosing frame visible.
If you have multiple button groups, it is a good idea to group them together
visually. You probably use panels to build up your user interface, but the panels
themselves are invisible. You can add a border to a panel to make it visible. In
Figure 4 , for example; the panels containing the Size radio buttons and Style check
boxes have borders.
You can place a border around a panel to group its contents visually.
Search WWH ::




Custom Search