Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using JButtons to move up and down a JRadioButton group  RSS feed

 
jin kazama
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There

Thank you for you kind assistance in advance.

I would like to use two JButtons (up and down) to move a JRadio group of 5 selection up and down.

I have used a ButtonGroup to bunch the JRadioButton's together, but I cant find a way to move up and down each time the JButton's are pressed.

What is the best way on the press of the up JButton for it select the next JRadioButton, and the select the next JRadioButton when pressed again?

Cheers

Jin Kazama

 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand the problem. Do you want to move the radio buttons, or do you want to alter the selection?

Moving to our GUIs forum, where we usually discuss such questions.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep an Array of the radio buttons. Then you search through the array to find which button is selected. For moving "down" the list you then get the next button in the array and make it selected. I'll let you figure out the code for moving "up".
 
jin kazama
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your advice, I will have a go with using an Array.

I have managed to get it to work with a Cast/Switch:

if (buttonPressed.equals(upButton)){
int currentButton = 1;
currentButton ++;

switch (currentButton){
case 1:
radioButton1.setSelected(true);
currentButton ++;
break;
case 2:
radioButton2.setSelected(true);
currentButton ++;
break;
case 3:
radioButton3.setSelected(true);
currentButton ++;
break;
case 4:
radioButton4.setSelected(true);
currentButton ++;
break;
case 5:
radioButton5.setSelected(true);
currentButton ++;
break;
}
}

Cheers

Jin Kazama
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code should only ever set radioButton2 to be selected. I'm sure that's not what you intended.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't like the case and switch. You ought to be adding a Listener object to your buttons, one with +1 as the movement from button to button, and th other with -1. Rob Camick has already told you what to do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!