Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ButtonGroup problem

 
James Lee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added 2 JRadioButton object to a ButtonGroup object. During execution of the program, one of the button is selected. Under certain condition, I set both buttons to be NOT selected by method setSelected(boolean). But the one selected before still get selected.
In order to achieve what I want, I remove the buttons from the group and the add them back. Is there anyone meet similar situation??
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can tell after doing numerous tests with ButtonGroup, ButtonModel, and JRadioButton, as well as reading the API, once a Button in a button group is selected, at least 1 button will remain selected no matter what. At least with JRadioButtons I believe this to be true.
BTW - This should be in SWING. Probably get better answers than mine. Hopefully a work around, or someone to tell me I am wrong.
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I preferred to do adding an Invisible Radio button to the ButtonGroup and set Selection to the Invisible one programatically if none other buttons want to be get Selected.
Say....
JRadioButton rad1=new JRadioButton();
JRadioButton rad2=new JRadioButton();
JRadioButton rad3=new JRadioButton();
rad3.setVisible(false);
ButtonGroup butGroup=new ButtonGroup();
buttGroup.add(rad1);
buttGroup.add(rad2);
buttGroup.add(rad3);
if(yourCondition){
rad3.setSelected(true);
}
 
James Lee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your approach may works, but it requires the program to 'swap' radio buttons under different conditioin. There's 3 scenario: (1) only rad1 selected, (2) only rad2 selected, (3) none of them get selected. And the button have their own text. If I 'swap' the component, may lead the code be a little bit complex. Have I misunderstand your approach?
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get exactly what the problem of doing this approach.Imagining u have some layout problem with that...if that is the case see below.
do u want to swap the components?if u are talking about layout problem while swaping(because i don't know what kind of layout you are using.another alternate instead of making third radio button invisible is don't add it to any container,but make sure that you added to some ButtonGroup object.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using an invible button to achieve that no (radio)button is selected, than it might be better to use check boxes rather than radio buttons. the user won't try to click on a check box to unselect it.
in case of grouped checkboxes you have to use the method setSelected(ButtonModel, boolean) of ButtonGroup to select the invisible button.
chantal
 
James Lee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkbox can be a good choice. But what I concern is that I want user to be allowed selecting only one of the choices. Actually, the buttons are on a Dialog window. When it 'pop up', I want to the buttons are either 'none of them get selected' or 'only one get selected'.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can group checkboxes just like radiobuttons. ButtonGroup is the class that controlls the selection mode of the buttons. if you add checkboxes to a button group they behave exactly as radio buttons would do.
Chantal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic