• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add text and image to JList

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
how can I add a string and an image to a jlist?

Thanks!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
reply
    Bookmark Topic Watch Topic
  • New Topic