views:

618

answers:

4

When displaying a group of JRadioButtons, initially none of them is selected (unless you programmatically enforce that). I would like to be able to put buttons back into that state even after the user already selected one, i.e., none of the buttons should be selected.

However, using the usual suspects doesn't deliver the required effect: calling 'setSelected(false)' on each button doesn't work. Interestingly, it does work when the buttons are not put into a ButtonGroup - unfortunately, the latter is required for JRadioButtons to be mutually exclusive.

Also, using the setSelected(ButtonModel, boolean) - method of javax.swing.ButtonGroup doesn't do what I want.

I've put together a small program to demonstrate the effect: two radio buttons and a JButton. Clicking the JButton should unselect the radio buttons so that the window looks exactly as it does when it first pops up.

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

/**
 * This class creates two radio buttons and a JButton. Initially, none
 * of the radio buttons is selected. Clicking on the JButton should
 * always return the radio buttons into that initial state, i.e.,
 * should disable both radio buttons.
 */
public class RadioTest implements ActionListener {
    /* create two radio buttons and a group */
    private JRadioButton button1 = new JRadioButton("button1");
    private JRadioButton button2 = new JRadioButton("button2");
    private ButtonGroup group = new ButtonGroup();

    /* clicking this button should unselect both button1 and button2 */
    private JButton unselectRadio = new JButton("Unselect radio buttons.");

    /* In the constructor, set up the group and event listening */
    public RadioTest() {
        /* put the radio buttons in a group so they become mutually
         * exclusive -- without this, unselecting actually works! */
        group.add(button1);
        group.add(button2);

        /* listen to clicks on 'unselectRadio' button */
        unselectRadio.addActionListener(this);
    }

    /* called when 'unselectRadio' is clicked */
    public void actionPerformed(ActionEvent e) {
        /* variant1: disable both buttons directly.
         * ...doesn't work */
        button1.setSelected(false);
        button2.setSelected(false);

        /* variant2: disable the selection via the button group.
         * ...doesn't work either */
        group.setSelected(group.getSelection(), false);
    }

    /* Test: create a JFrame which displays the two radio buttons and
     * the unselect-button */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        RadioTest test = new RadioTest();

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(3,1));
        contentPane.add(test.button1);
        contentPane.add(test.button2);
        contentPane.add(test.unselectRadio);

        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

Any ideas anyone? Thanks!

+2  A: 

Try adding a third invisible button to the button group. When you want to "deselect", select the invisible one.

Ash
Same answer as sateesh's - thanks!
Thomas
+2  A: 

The Javadoc of the class ButtonGroup itself gives some hint about how this can be achieved:

From the API doc of class 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.

sateesh
What nonsense... but thanks a lot for the answer!
Thomas
Oh my... I thought something was strange, as I had read through the API documentation before. Now, get this: turns out the part you quoted has been *removed* from the API documentation with Java 6! See here for a comparison: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ButtonGroup.html (Java 5) and http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html (Java 6).No wonder I wasn't able to find anything in the documentation...
Thomas
@Thomas - I agree. Nonsense. Bleh.
JasCav
+2  A: 

Or you can use Darryl's Select Button Group which doesn't require you to use an "invisible button".

camickr
+1  A: 

You can do buttonGroup.clearSelection().

but this method is available in java6 only.

http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html#clearSelection()

Santhosh Kumar T
Awesome! That explains why the Javadoc for ButtonGroup removed the part about the invisible RadioButton... (Now, if only they had *added* a part about clearSelection ;-))
Thomas