Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage two JRadioButtons in java so that only one of them can be selected at a time

Tags:

How to manage two JRadioButtons in java so that only one of them can be selected at a time? Is there any method in java to take care of this or you need to build your own logic?

like image 956
stillStudent Avatar asked Feb 12 '10 17:02

stillStudent


People also ask

How do I make it so I can only select one radio button in Java?

We add radio buttons in a ButtonGroup so that we can select only one radio button at a time. We use “ButtonGroup” class to create a ButtonGroup and add radio button in a group. Methods Used : JRadioButton() : Creates a unselected RadioButton with no text.

Which class do you use if you want that only one of some radio buttons can be selected at a time?

Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected. The Swing release supports radio buttons with the JRadioButton and ButtonGroup classes. To put a radio button in a menu, use the JRadioButtonMenuItem class.

What is the use of button group?

The ButtonGroup component manages the selected/unselected state for a set of buttons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time. Initially, all buttons managed by a ButtonGroup instance are unselected.

What is JRadioButton in Java?

The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz. It should be added in ButtonGroup to select one radio button only.


1 Answers

You have to add them in a ButtonGroup

ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton); 

Ensure you add this code after the buttons are created using the new JRadioButton constructors, as appropriate.

like image 98
ccheneson Avatar answered Sep 20 '22 12:09

ccheneson