ButtonGroup Class
Package: javax.swing
The ButtonGroup
class creates a group in which you can add radio buttons. Of course, only one radio button in a button group can be selected at any time. Thus, selecting a radio button in a button group automatically deselects any other buttons in the group.
Constructor
Constructor |
Description |
|
Creates an empty button group |
Method
Method |
Description |
|
Adds a button to the group |
Before you create a button group, you should create the buttons you will add to it. Here’s an example that creates three radio buttons named small
, medium
, and large
:
JRadioButton small, medium, large;
Now that you have buttons, you can create a button group by calling the ButtonGroup
class constructor. Then, you can add the buttons to the button group by calling the add
method. For example:
ButtonGroup group1 = new ButtonGroup();
group1.add(small);
group1.add(medium);
group1.add(large);
Get Java For Dummies Quick Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.