File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes ButtonGroup problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ButtonGroup problem" Watch "ButtonGroup problem" New topic
Author

ButtonGroup problem

James Lee
Greenhorn

Joined: Nov 24, 2002
Posts: 11
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Bhagya Tangutur
Ranch Hand

Joined: Oct 21, 2002
Posts: 88
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);
}


Sun Certified Java Programmer
James Lee
Greenhorn

Joined: Nov 24, 2002
Posts: 11
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

Joined: Oct 21, 2002
Posts: 88
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

Joined: Sep 28, 2000
Posts: 508
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

Joined: Nov 24, 2002
Posts: 11
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

Joined: Sep 28, 2000
Posts: 508
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ButtonGroup problem
 
Similar Threads
two really important questions
JRadioButtonList error
JRadioButton : Deselecting
deselect elements belonging to the same ButtonGroup
model for group of radio buttons