tags:

views:

78

answers:

5

I have 4 checkboxes added as a group so that I will be able to select only one checkbox. But if I select one of them and now want to uncheck all of them in a group, I am unable to do that. Any one has idea how can I do that?

A: 

You are describing the behavior of a RadioButton, which is a selection group which only allows one option to be selected at a given time; support for this is built into Swing.


EDIT: I misread the question.

To deselect all checkboxes you can iterate the Containers children or, easier, use ButtonGroup.getElements() to enumerate the buttons in a particular group.

Software Monkey
@software monkey: Yeah you are right about the support which allow you to select only one button at a time but what if now you want to deselect all the buttons in group? This is what I am asking.
newbee
A: 

--[EDIT]--
I made a mistake, I answered this thinking it was about a C# .NET project of another question I was reading. Vote to close it if you will, but read this note before choosing to downvote.
--[/EDIT]--

For what you said afterwards, you wand radioboxes that could act as checkboxes (re-clicking the same should un-check it). For that, you could use radiobuttons and add an option named "none" that will act as if no option was select, or put a "clean" button that de-selects every radiobutton.

Or if you REALLY want to do this, you could use CheckBox_CheckedChanged handler, which is triggered everytime a the checkbox is checked OR unchecked, so you'd have to see if the checkbox is checked inside the handler. The code inside the handler Should look like this:

If (CheckBox1.Checked)
{
    CheckBox2.Checked = False;
    CheckBox3.Checked = False;
    CheckBox4.Checked = False;
    CheckBox5.Checked = False;
}
// same for other checkboxes

Or make a function that takes the currently checked checkbox and unchecks the others, and put it inside each handler, each sending the desired checkbox.

But I highly sugest you to choose one of the first solutions I gave..

MarceloRamires
Just noticed.. it's a JAVA question.. what am I doing in here? haha
MarceloRamires
+2  A: 

You can use clearSelection(), which "Clears the selection such that none of the buttons in the ButtonGroup are selected."

As an aside, using JRadioButton in a single-selection group might be less confusing than check boxes.

Addendum: @Joe Carnahan and @Chris suggested clear ways to make the "no selection" choice explicit. You might also look at Chapter 10 of the Java Look and Feel Design Guidelines, which discusses independent v. exclusive choice in connection with toggle buttons generally.

trashgod
+2  A: 

The behavior you're asking about is half-check-box and half-radio-button, and here's how you do it right: Make it a group of five radio buttons, the first of which (or last) basically says 'none of the other options'.

Chris
+1 for clarity.
trashgod
+2  A: 

Building on @trashgod's answer, here is a runnable example of code where a JButton is hooked up to clear the selection from a group of JRadioButton objects using ButtonGroup.clearSelection():

public class ClearableRadioButtons extends JFrame {

    public ClearableRadioButtons() {
        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        final ButtonGroup group = new ButtonGroup();
        for (int i = 1; i < 10; i++) {
            JRadioButton nextButton = new JRadioButton("Button " + i);
            group.add(nextButton);
            content.add(nextButton);
        }
        content.add(new JButton(new AbstractAction("Clear") {
            public void actionPerformed(ActionEvent arg0) {
                // This is the one line you really care about:
                group.clearSelection();
            } 
        }));

        setLayout(new BorderLayout());
        add(content, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ClearableRadioButtons();
            }
        });
    }
}

Note that this code will work equally well if you replace all the instances of JRadioButton with JCheckBox. However, this is not recommended, because the standard way to indicate that only one thing can be chosen is to use radio buttons rather than checkboxes. If the user sees checkboxes, users will generally expect to be allowed to select more than one, and they will become annoyed when selecting one of the checkboxes causes other ones to become unselected.

If you really don't like the way JRadioButton buttons look, then I suggest playing with your Swing look-and-feel to change all of your checkboxes and radio buttons in your application until you are happy with the result.

Joe Carnahan
+1 good example and interesting alternative to suggestion by @Chris.
trashgod
I'm not sure why this is receiving upvotes. It may work, but it won't work well. There is a very simple and long-accepted way to do what @newbee wants, and I've described it in my answer. Anything else and you will confuse the user because you're making controls that behave differently from the controls they've seen again and again in other applications, on webpages, and even on paper. This makes your product harder to use and it will cost you.
Chris
@Chris: I'm confused - I told @newbee to should use JRadioButton instead of JCheckBox, and I warned against any violation of the standard behavior for radio buttons (0 or 1 selected) versus checkboxes (any number may be selected). Where exactly did I advise @newbee to "make controls that behave differently from the controls they've seen again and again"? I certainly did not intend to suggest any such thing, and if I did so inadvertently, I'll edit my answer to remove that suggestion.
Joe Carnahan
Using a button to clear radio buttons is non-standard behavior. In fact, clearing radio buttons at all is non-standard, as radio buttons are designed so that at least one option has to be selected.
Chris
I think this is a grey area. I have definitely seen radio button controls (including car radios, from which the metaphor originally comes!) where no button is pressed. True, depending on the application, the "no button pressed" state is often only permissible either as an initial state or not at all. However, I think that restriction depends on the application. Meanwhile, the important thing about a radio button is just that selecting one option deselects the others.
Joe Carnahan