• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image background for a JComboBox  RSS feed

 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks for all the help on this one. I really appreciate it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!