button group

Posted on 2001-08-14
Last Modified: 2012-08-14
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.
Question by:raga

Expert Comment

ID: 6383847
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.

Author Comment

ID: 6383870
I don't want to remove the ButtonGroup, is there any method to do this?

Author Comment

ID: 6383903
I don't want to remove the ButtonGroup, is there any method to do this?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6384561
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.


Expert Comment

ID: 6384630
You don't even need to set it to invisible. Wouldn't it work if you just didn't add it to your Container?
LVL 19

Accepted Solution

Jim Cakalic earned 50 total points
ID: 6384743
The Java Tutorial provides this recommendation that seems to work:
"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."

The javadoc for ButtonGroup also suggests:
"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."

Of the two, the first method seems to be simpler. The only downside I can think of is it may not necessarily be portable or might fail to work in future versions of Swing.

Best regards,
Jim Cakalic

Expert Comment

ID: 6386645
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.

Author Comment

ID: 6387684
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.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
numbers ascending pyramid 101 214
SHA2 certs for IIS AND Java? 2 113
type mismatch (Object[] to double[] 4 33
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question