Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

howto unselect a toggle button

Status
Not open for further replies.

mes123

Programmer
Jul 29, 2005
62
GB
I have a button group containing several toggle buttons.

How do I unselect a button? I've tried togglebutton.setSelected(false) and togglebutton.doClick() but the button remains selected.
 
According to the Sun, from JDK1.4 onwards ...

there's no supported API for unselecting all the buttons.


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
drat - how about unselecting a single one? I can mod the code easily so i know which button was selected.
 
But it also implies that you can't unselect a single one, since unselecting the one which is selected would leave them all unselected [smile].

To unselect the selected one, you'd have to select one of the others.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
ah yes, you're right there.

Well, I've implemented a bodge to get round the problem. I've created an extra button in the group and set it's size to 0 so it can't be seen. By setting this hidden button as selected it effectivly clears the rest.

A horrible solution but it gets round the problem for now.
 
Glad you've got a solution to take you forward.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
From Sun:

Code:
You should generally initialize a group of radio buttons so that one is selected. However, the API doesn't enforce this rule -- a group of radio buttons can have no initial selection. Once the user has made a selection, exactly one button is selected from then on. There's no supported API for unselecting all the buttons. However, if you really want to unselect all the buttons (not that we recommend it), invoking setSelected(null, true) on the ButtonGroup should do the trick.

Cheers,
Dian
 
I suppose it all depends on whether you want a way which will keep on working as opposed to a technique which even Sun don't recommend and therefore may simply break when upgrading your JDK.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
it's a difficult one. The toggle buttons form a login page on a POS system, a user selects their ID before proceeding, after the sale the login is displayed again and obviously it's better if all the buttons are deselected.

I suppose I could replace them with normal jButtons and impletment the toggle functionality manually, it's just that the toggleButton group seemed the idea choice.

Thanks anyhow guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top