Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Image background for a JComboBox

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
On a new project I'm starting, I thought it would be really cool if I could set the background of the JComboBox I'll be using to a particular image, the same way you can set a JPanel background to an image.
1) Is this insane idea actually possible?
2) Does implementing this affect the performance of the JComboBox (is appearance/scrolling jittery or smooth, for example)?
Thanks.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
Its definitely worth a shot. Try overriding the paintComponent() method within the JComboBox and drawing an Image instead. You can load up an image file into an Image object using Toolkit.getDefaultToolkit().createImage() and draw it using Graphics.drawImage() - check the documentation.
I imagine that performance of the combo box would be affected, but I'm not sure by how much.
Good luck.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Jim
I think you mean drawing an image within the popu menu that appears when we click the arrow in the combobox. But how do we access to it?
[ November 10, 2003: Message edited by: Jose Botella ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Provide a ListCellRenderer implementation for the JComboBox:
combobox.setRenderer(MyRenderer);
Make sure combobox is not editable, otherwise an editor will show up.
The implementation of the interface returns itself from the method getListCellRendererComponent(...)
Extending from JLabel it will draw its icon as all the labels do. The text of the label is taken from the value parameter. This is the content of the cell that the ListCellRenderer is err... rendering.
See page 242 of Swing 2nd ed, by Matthew Robinson and Pavel Vorobiev for a more complete example.
 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help on this one. I really appreciate it.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic