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

ButtonGroup()

Creates an empty button group

Method

Method

Description

void add(Abstract Button button)

Adds a button to the group

tip.eps You’ll usually add only radio buttons to a button group, but you can add regular buttons or check boxes, too.

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);

CrossRef.eps Button groups have nothing to do with the visual appearance of the buttons on the screen. Instead, button groups simply provide a logical grouping for the buttons. To visually group the buttons together, ...

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.