Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deselect elements belonging to the same ButtonGroup

 
axel axek
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is there any way to deselect all CheckBoxes belonging to the same ButtonGroup?

Quote from javax.swing.ButtonGroup
"Initially, all buttons in the group are unselected. Once any button is selected, one button is always selected in the group. There is no way to turn a button programmatically to "off", in order to clear the button group. To give the appearance of "none selected", add an invisible radio button to the group and then programmatically select that button to turn off all the displayed radio buttons. For example, a normal button with the label "none" could be wired to select the invisible radio button."



...so....it may be possible by using an "invisible" check box?.... I just do not see how....

Any ideas?

Best Regards,
Alex
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the button to the group and don't add it to a visible Component.
 
axel axek
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and how would that button be selected if it is invisible?

...i mean...what dependencies could be set in order to clear the ButtonGroup when clicking on a selected checkbox?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using an old obsolete version of Java? Update to 1.6 and you have ButtonGroup#clearSelection()
Clears the selection such that none of the buttons in the ButtonGroup are selected.

 
axel axek
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...i know that there is such a feature.....but....the question is....what would trigger the clearSelection() on the button group?

Supposing that a GUI only has 3 check Boxes belonging to the same button Group, clicking on one of them will select the desire check Box, but when clicking again on the same check box in order to deselect it, it won't...
...how can the clearSelection() method be used in this case? (you can't trigger it by a check box in this case.....)
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposing that a GUI only has 3 check Boxes belonging to the same button Group, clicking on one of them will select the desire check Box, but when clicking again on the same check box in order to deselect it, it won't...

It can. Use an extended ButtonGroup and override setSelected(...) to produce the desired behavior.
 
axel axek
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice

I haven't thought of it this way...

Thank you!


....
A life to learn everything about Java is not enough...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic