tags:

views:

666

answers:

6

I could really use a tri-stated checkbox in Java. It sounds like a simple thing, but I've only seen really ugly implementations.

Three radio buttons just take up too much real estate and will probably be confusing for the users in my case. It's basically for a search dialog. I need true, false or "don't care" options. Is there a different technique that people use?

+11  A: 

Use a drop-down.

Gandalf
+1 good idea. if a UI component is hard to code and nonstandard, it will feel that way to the user as well. When was the last time you used a tri-state checkbox in that way? I never have.
Byron Whitlock
Although I agree with @byron's point, a tri-state check box is actually fairly standard from a users point of view.
Bill K
Which users? I'm a developer and power user, and I despise tri-state checkboxes, usually because it's not clear what the third state means; typically, it means some detail is being hidden from you. It's a usability and accessibility nightmare.
Rob
Most of the time it means that the checkbox applies to a class of things and that those things have different states and you don't want them changed. Checking it checks them all, leave it in the third state to not change it. Very common even for power users.
Bill K
Typical usage of tri-state checkboxes: changing properties on multi object selection, grayed+selected third state meaning "some items have this property and some have not, and I won't change any of them". Most common in Microsoft applications.
Nicolas Simonet
Most usefull use for a tristate is this: No state, On and off.When you use the standard 2state you only have on and off and when you want to make sure the user actually choose one you need the third state,
Peter
A: 

I'd just use the one you posted.

As long as your complexity is in another class (that works) and it acts just like any other control, who cares? (That seems to be the assumption behind all of swing, most swing classes seem to be about this complicated.)

Bill K
+1  A: 

That "ugly implementations" is an old link. One of the suggestions on that page was updated a couple of years ago. I haven't tested the old implementation, so I don't know if the new one is any better or worse.

TristateCheckBox Revisited

camickr
Yes, i think Heinz Kabutz, done this very well. I would also recommend that!
codedevour
A: 

Change the UI. Tristate check-box is unusual and can really confuse users. The drop down is good option but for more then one occurrence within dialog it will also bring a lot of confusion to user.

Rastislav Komara
A: 

I found a way to make a tri-state checkbox by simply adding a listener:


public class TriStateActionListener implements ActionListener{
    final protected Icon icon;

    public TriStateActionListener(Icon icon){
        this.icon=icon;
    }

    public static Boolean getState(javax.swing.JCheckBox cb){
        if (cb.getIcon()==null) return null;
        if (cb.isSelected()) return true;
        else return false;
    }

    public void actionPerformed(ActionEvent e) {
        javax.swing.JCheckBox cb=(javax.swing.JCheckBox)e.getSource();
        if (!cb.isSelected()){
            cb.setIcon(icon);
        }
        else if (cb.getIcon()!=null){
            cb.setIcon(null);
            cb.setSelected(false);
        }
    }
}

Then in the application code, it's just a single line:


jCheckBox1.addActionListener(new TriStateActionListener(getResourceMap().getIcon("TriStateIcon")));

After all the feedback, I'm thinking a drop-down may be a better choice. But, I wanted to share my code here for everyone else.

User1
+1  A: 

Tristate check-boxes are standard UI idiom for Treeviews with partially checked children nodes. They are widely used in layer management in complex hierarchial views such as Google Earth.

whatnick