User999
User999

Reputation: 19

JradioButtons unchecking

I have a menu bar with two menus on it.

On one of the Menus I have difficulities Easy,Medium,Hard.

When clicking on several of these radiobuttons they all stay checked. My problem is: How do I uncheck them and only make sure that one of the buttons can remain checked at a time?

I have tried this but it doesnt seem to work.

 if (Easy.isSelected() == (true)) 
 {
    Medium.setSelected(false);
    Hard.setSelected(false);        
 }

 if (Medium.isSelected() == (true)) 
 {
    Easy.setSelected(false);
    Hard.setSelected(false);    
 }

 if (Hard.isSelected() == (true)) 
 {
    Easy.setSelected(false);
    Medium.setSelected(false);
 }

Upvotes: 0

Views: 216

Answers (2)

camickr
camickr

Reputation: 324098

Read the section from the Swing tutorial on How to Use a Button Group. It also contains a link on How to Use Radio Buttons for a code example.

Upvotes: 3

Radu Murzea
Radu Murzea

Reputation: 10900

You need to put all 3 buttons in a ButtonGroup. See here for an example about how to use radio buttons in Swing.

Upvotes: 2

Related Questions