File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Showing a string and then an icon at the same time in a JComboBox. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Showing a string and then an icon at the same time in a JComboBox." Watch "Showing a string and then an icon at the same time in a JComboBox." New topic
Author

Showing a string and then an icon at the same time in a JComboBox.

wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
I am attempting to have a JcomboBox with a list of ranks to choose but i would like to have a small picture of that rank alongside the text.

I can get the text as an option, i can also get a picture as an option but not both at the same time.

I guess the obvious question is, is it possible in the first place as all the examples i have found appear to have the picture output as effectively a separate window attached to the JComboBox?

The basic idea i have been trying is




The result is
RookieRookie.gif
appears in the JComboBox.i.e. the string and then the file reference rather than the picture itself.

Any pointers on how this could be achieved would be appreciated.

Thank You.


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

Hello there

You are adding an "object" to your JComboBox. Yet your image and string are 2 different objects. When you do this, the image object is implicitly converting to string hence the filename rather than the image itself.

I suggest 1) use a model when creating your combo box, or 2) use a Javabean embedding the image and description, or 3) use an array.

This link should help you.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

You need 2 things:
1) You need to have a separate class that will hold the image and string value. You can then add this object to the addItem() method.
2) Then, you need to write a Renderer - A renderer controls how the items are 'displayed' - you can tweak the display as per your needs.

To know more about renderers, check out the tutorial


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
That's great information. The tutorial is particularly useful.

Thank you both for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Showing a string and then an icon at the same time in a JComboBox.
 
Similar Threads
jComboBox Help
Picture Woes
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI
Display an image for a set time
I can't load a file with getResource