I could really use a tri-stated checkbox in Java. It sounds like a simple thing, but I've only seen really ugly implementations [note: link now broken].
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?
I found a way to make a tri-state checkbox by simply adding a listener:
Then in the application code, it's just a single line:
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.
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.
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.
In this implementation the three state can be only set via programmatically. To be Look and Feel portable it use images, that must be placed inside the the same java package.
Sample frame:
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
I dont know why anyone would give the solutions with additional icon png files while java api gives great funcionality for overriding paintIcon(..) method.
The best lightweight solution to remember CheckBox state is IMO ClientProperty attribute.
usage:
TCheckBox tcb = new TCheckBox("My CheckBox");