aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Add text and image to JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add text and image to JList" Watch "Add text and image to JList" New topic
Author

Add text and image to JList

Kay Tracid
Ranch Hand

Joined: Mar 06, 2002
Posts: 148
Hi,
how can I add a string and an image to a jlist?

Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Here is an example on Sun's Swing Tutorial.
Althought it is part of the JComboBox tutorial, Implementing ListCellRenderer is done the exact same on both a JComboBox and a JList.
That should be what you want.


GenRocket - A Test Data Generation Platform
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
Going with what Gregg said, what about using a label for the elements in the list. I think labels can hold both an image and text by nature, so if it satisfied what you needed, it might be easier than constructing something special. Plus, you may not have to create a special ListCellRenderer for labels. May just be able to add them. Have to check that tho.
HTH
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Chris has a point, but that can make retrieving the values a little more difficult, since you need to retrieve text typically, and not a lable.
ListCellRenderer extends JLabel (which you could really extends whatever component you wanted) but for what you want, take the ListCellRenderer as is, and modify it to place your images for you, rather than adding Labels to the List that contains images and text. That way, the method to retrieve the value you need from the list doesn't change.
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
yeah but sucking the String out of a JLabel is as simple as getText().
Just do this:
now s is your selected text value. Or if your options are always the same with the same order, you could just check the list.getSelectedIndex() and perform the apropriate action based on the int returned. That actually saves the time comparing the text strings we collected using my other method.
HTH
Kay Tracid
Ranch Hand

Joined: Mar 06, 2002
Posts: 148
Thanks for your help. What should I use to display 4-5 lines of text and an image? i think with a label I have only one line and it is bad to format. What do you think is the best way?
Thanks in advance
[ February 22, 2003: Message edited by: Kay Tracid ]
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
oh, well then labels are out. Maybe do something with a customized list cell renderer like what Gregg suggested. Are you sure that you need all the extra text in your list? Any way to cut it down to a single line? Without knowing what you are trying to acheive tho, the listcellrenderer sounds like the way to go...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add text and image to JList
 
Similar Threads
Listening on Checkbox rendering in JLlist
Background Image in JList
regarding jFileChooser
JList
show multiple image Using Jlist