Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2004

Tip: Looking for answers? Try searching our database.

3-State Swing Checkbox

Thread view: 
Chas Douglass - 28 Dec 2004 16:58 GMT
Googling hasn't found me anything useful -- but perhaps I'm not coming up
with good search terms...

I'm looking for a Swing "3-state checkbox".  In this particular case the
three states are "unknown", "yes", and "no" but the general applicability
is, I think, obvious.

Windows does something similar with a checkbox that is greyed for "not
selected" (or sometimes meaning "do not override").

Thanks in advance,

Chas Douglass
Andrew Thompson - 28 Dec 2004 17:33 GMT
> I'm looking for a Swing "3-state checkbox".  In this particular case the
> three states are "unknown", "yes", and "no" ...

<sscce>
import java.awt.*;
import javax.swing.*;

/** Demonstrates a three state checkbox. */
class ThreeStateCheckbox {

 public static void main(String args[]) {

   JFrame f = new JFrame("Three State Checkbox");
   Container c = f.getContentPane();
   c.setLayout(new GridLayout(0,1));

   ButtonGroup bg = new ButtonGroup();

   JCheckBox yes = new JCheckBox("Yes");
   bg.add( yes );
   c.add( yes );

   JCheckBox no = new JCheckBox("No");
   bg.add( no );
   c.add( no );

   JCheckBox unknown = new JCheckBox("Unknown");
   bg.add( unknown );
   c.add( unknown );

   f.pack();
   f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   f.setVisible(true);
 }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Chas Douglass - 28 Dec 2004 18:40 GMT
>> I'm looking for a Swing "3-state checkbox".  In this particular case
>> the three states are "unknown", "yes", and "no" ...
>
><sscce>
[snip]

Thanks, but I'm really looking for a single control. It needs to be placed
in a JTable (well, actually, several of them will get placed in a single
JTable) and "real estate" is an issue.

Chas Douglass
Andrei Kouznetsov - 28 Dec 2004 22:09 GMT
>>> I'm looking for a Swing "3-state checkbox".  In this particular case
>>> the three states are "unknown", "yes", and "no" ...
[quoted text clipped - 5 lines]
> in a JTable (well, actually, several of them will get placed in a single
> JTable) and "real estate" is an issue.

why you don't want to use simple JComboBox with 3 entries?

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Chas Douglass - 28 Dec 2004 23:42 GMT
>>>> I'm looking for a Swing "3-state checkbox".  In this particular
>>>> case the three states are "unknown", "yes", and "no" ...
[quoted text clipped - 7 lines]
>
> why you don't want to use simple JComboBox with 3 entries?

Strictly aesthetics.  I think a 3-state checkbox (again, similar to what
I've seen in Windows property sheets, which can be checked, unchecked, or
"sort of grayed" (but not disabled) which means "inherited").

I'm sorry I can't remember exactly where I've seen that -- I thought it was
a fairly common UI pattern.

Chas Douglass
Andrei Kouznetsov - 29 Dec 2004 00:58 GMT
>>>>> I'm looking for a Swing "3-state checkbox".  In this particular
>>>>> case the three states are "unknown", "yes", and "no" ...
[quoted text clipped - 15 lines]
> was
> a fairly common UI pattern.

try this link: http://www.javaspecialists.co.za/archive/Issue082.html

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Chas Douglass - 29 Dec 2004 20:41 GMT
> try this link: http://www.javaspecialists.co.za/archive/Issue082.html

Thanks, I'll give this a try.  I appreciate the response.

Chas Douglass
Andrew Thompson - 29 Dec 2004 13:57 GMT
> .."real estate" is an issue.

Well, fussy, fussy!  OK, try this..

<sscce>
import java.awt.*;
import javax.swing.*;

/** Demonstrates a three state checkbox, that's small. */
class ThreeStateCheckbox {

 public static void main(String args[]) {

   JFrame f = new JFrame("Three State Checkbox");
   Container c = f.getContentPane();
   c.setLayout(new GridLayout(1,0));

   ButtonGroup bg = new ButtonGroup();

   JCheckBox yes = new JCheckBox("Y");
   yes.setToolTipText("Yes, for sure, feel free..");
   bg.add( yes );
   c.add( yes );

   JCheckBox no = new JCheckBox("N");
   no.setToolTipText("No way Jose!");
   bg.add( no );
   c.add( no );

   JCheckBox unknown = new JCheckBox("?", true);
   unknown.setToolTipText("Don't ask me, I just work here..");
   bg.add( unknown );
   c.add( unknown );

   f.pack();
   f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   f.setVisible(true);
 }
}
</sscce>

Of course, that'll 'cost' you double.    ;-)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Chas Douglass - 29 Dec 2004 20:42 GMT
>> .."real estate" is an issue.
>
[quoted text clipped - 38 lines]
>
> Of course, that'll 'cost' you double.    ;-)

Thanks for the effort, Andrew, but it's still not quite what I was hoping
to find.

I appreciate it though, it is a good backup plan.

Chas Douglass


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.