0

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);
 }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
User999
  • 21
  • 5
  • 2
    Follow Java naming conventions. Variable names should NOT start with an upper case character (ie. "Hard" should be "hard"). – camickr Oct 07 '13 at 17:10

2 Answers2

3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • How do i get the Buttongroup to be added as a JMenuItem? – User999 Oct 07 '13 at 17:15
  • A ButtonGroup is NOT a comonent, so you don't add it to anything. Did you read the tutorial and download the working example? You can also read the section from the Swing tutorial on `How to Use Menus` for a working example of how to use radio buttons in a JMenu. I gave you a link to the tutorial so you can do some basic reading so you don't have to depend on us to answer all your questions. – camickr Oct 07 '13 at 17:21
  • 1
    What do you mean "your own way"? If your code is not like the code in the tutorial then it is probably wrong. Learn to do things properly to reduces programming bugs. – camickr Oct 07 '13 at 17:26
  • It's Working as it should... I added the three difficulties as JMenuItems to the JMenu normally and from there created a buttongroup adding the JMenuItems to it. – User999 Oct 07 '13 at 17:29
  • `I added the three difficulties as JMenuItems` - you should not be using JMenuItem. You should be using `JRadioButtonMenuItem`. This is clearly demonstrated in the tutorial. – camickr Oct 07 '13 at 17:32
  • So it is not your way, it is the way the tutorial suggested, the question should be closed. – camickr Oct 07 '13 at 17:38
  • Well.. all i did was just take a close look at the buttongroup and ignored the rest, and then i tried on my own, i learn better that way. So that's basically what i meant on my own way. But anyways let's not argue about this anymore. The problem is solved and that's what's important. – User999 Oct 07 '13 at 18:07
  • @WilliamBergendahl `The problem is solved and that's what's important.` - what is important is that you take the time to thank people who helped you. In this forum that is done by upvoting/accepting answers. We pointed you in the direction of the ButtonGroup. – camickr Oct 07 '13 at 19:01
  • I like that you said "we". People appreciate details like that ;) – Radu Murzea Oct 08 '13 at 07:18
2

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

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69