This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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??
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.
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?
Joined: Oct 21, 2002
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.
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
Joined: Nov 24, 2002
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'.
Joined: Sep 28, 2000
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