Link to home
Start Free TrialLog in
Avatar of raga
raga

asked on

button group

I am using a ButtonGroup, according to java, initially no button is selected in the group. After that i am selecting any one of the button and after that i want to deselect all of the buttons , its somewhat like situation at the start i.e., all buttons unselected in the button group. But i heard that its not possible, does any one know about this?. Don't give the solution of using setSelected method, as this method is only applicable to button, not for ButtonGroup.
Avatar of exorcist
exorcist

I think this is true, you cannot deselect all buttons. But maybe it helps to remove all buttons from the ButtonGroup, deselect each of them and then add them to the ButtonGroup (or a new ButtonGroup).

Just a thought.
Avatar of raga

ASKER

I don't want to remove the ButtonGroup, is there any method to do this?
Avatar of raga

ASKER

I don't want to remove the ButtonGroup, is there any method to do this?
You can try to fool buttongroup by adding a dummy button, setting it to selected using buttonGroup.setSelected( dummyButton.getModel(), true ) and then removing it.

I didn't try this (because I didn't need it), so I don't know if it will work, also I don't know when you remove dummy button will button group select something else.

Also, you could have this button all the time in the group, but set it to invisible, and select it programatically when you want to deselect the group.

Greetings,
    Ntr:)
You don't even need to set it to invisible. Wouldn't it work if you just didn't add it to your Container?
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jim is right. All you need to do is to create an 'invisible' button and then you add it to your button group. However, you don't add this button to your panel, dialog or frame. This way, it will not appear at all. When you want to unselect all the visible buttons, you just need to select the 'invisible' button.
Avatar of raga

ASKER

Actually, we were using 1st suggestion till now, but after modifying the layout and some gui in which the buttongroup is added, it was giving exception. I tried the second one it is working fine.