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 / March 2005

Tip: Looking for answers? Try searching our database.

Q:How to add JCheckBox to JComboBox to get a dropdown of JCheckBox'es

Thread view: 
Birkemose - 15 Mar 2005 22:18 GMT
Tried

.add( myCheckBox )
.validate( )

Combo still empty

.addItem( myCheckBox )
.validate( )

Combo contains text ala "javax.swing.JCheckBox[......."

TIA
John McGrath - 16 Mar 2005 01:04 GMT
> Tried
>
[quoted text clipped - 7 lines]
>
> Combo contains text ala "javax.swing.JCheckBox[......."

Flippant reply:

I would have expected that you would get a compiler error with the above
code.  You need an object reference or class name before the ".".

Serious reply:

If you want some help with a problem you are having, you ought to provide
a description of what you are trying to accomplish, what you have tried,
including *real* code samples, a description of what the result was and
how it differs from what you expected.  If you want someone to take the
time to help you, you should be willing to take the time to write out a
description with complete, legible sentences.

That said, I am guessing that you are adding checkboxes to a combo box and
expecting them to render and function as checkboxes.  The items in a combo
box are not components, so they do not work that way.  Check out the Swing
Tutorial trail on combo boxes:

 http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html

Signature

Regards,

John McGrath

Birkemose - 17 Mar 2005 16:51 GMT
"John McGrath" wrote...

> Flippant reply:
<snip>

Thanks for the answer.... not sure why you think the info I provided was
lacking, what part did you not understand ;o)
John McGrath - 17 Mar 2005 18:10 GMT
> "John McGrath" wrote...
>
[quoted text clipped - 3 lines]
> not sure why you think the info I provided was lacking, what part did
> you not understand ;o)

> > .add( myCheckBox )
> > .validate( )

What is this myCheckBox?  Is it an AWT Checkbox, a swing JCheckbox, or
perhaps something else?  What is myCheckbox being added to and what is
being validated?

> > Combo still empty

What combo?  Nobody mentioned a combo before?  Why should it not be empty?
What were you expecting it to contain?

> > .addItem( myCheckBox )
> > .validate( )

> > Combo contains text ala "javax.swing.JCheckBox[......."

Again, what is this adding to and validating?

I was able to figure out what you were trying to do, but it probably took
me 3 or 4 times as long as it would have had you provided a reasonable
description of the problem.  I had plenty of time, and I was avoiding
doing something else that I did not want to do, so I took the extra time.
Under other circumstances, I would have probably ignored the message.

Signature

Regards,

John McGrath

Birkemose - 19 Mar 2005 13:40 GMT
<snip>
> I was able to figure out what you were trying to do, but it probably took
> me 3 or 4 times as long as it would have had you provided a reasonable
> description of the problem.  I had plenty of time, and I was avoiding
> doing something else that I did not want to do, so I took the extra time.
> Under other circumstances, I would have probably ignored the message.

Hmmm... point taken..... maybe I should get out more ;)
Chris Smith - 16 Mar 2005 04:11 GMT
> Tried
>
[quoted text clipped - 7 lines]
>
> Combo contains text ala "javax.swing.JCheckBox[......."

JComboBox can contain items of any class, but by default they still
display as text.  If you want them to display differently, then the
correct place to look is the *renderer*, not the actual object that's
been added.  So the actual objects in the JComboBox can be of any type,
and you'd implement a renderer that shows them as check boxes.

Renderers are a strange beast, and you should understand how they work
before using them, or you will run into all manner of weird things.  
Although you can return a new component each time you're asked for a
renderer, the normal approach is to use one component, and just keep
modifying its properties to act as a "rubber stamp" for each item.  So
you could have:

public class MyRenderer extends JCheckBox implements ListCellRenderer
{
   ...
}

and getListCellRendererComponent would set up the state of the object
(by calling this.setSelected, this.setText, etc.) and then just
"return this".

Note that a renderer only affects how the combo box items look.  You'll
need your own logic for how they change state.  Clicking on an item in
the drop-down list for a combo box generally only selects that item,
rather than changing its state.  However, you could add an ItemListener
to change the state, or you could do something more elaborate and
intuitive with MouseListener instead.

Hope that helps,

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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.