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);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access