• 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

JComboBox item rendering

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a JComboBox which has a list of string items.I have a requirement,wherein on changing the default selected item of the ComboBox to some other item ...the new selected item should be colored red on the foreground and yellow on the background.
am using a renderer ,extends DefaultListCellRenderer,which runs whenever a new item is selected ,to paint the foreground and background with respective colors.it works fine
the code of my renderer is as follows:

the problem occurs whenever i click on some other control on the screen,the applied coloring just disappears and default coloring appears.But if i click on the same combo box again the apllied coloring reappears.
any suggestion/solution for it?...i want the coloring to stay even if i click anywhere else on the screen.
Regards
Surajeet
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use this ListRenderer instead...

 
surajeet dev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nate,
thanks for replying....i have already tried out
the renderer with the code u provided but thats not my requirement,
i want the combo box item to remain painted with the specified colors...even after it looses focus...(at present with my code it looses the color once i click on any other component on the screen)...
Regards
Surajeet
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then what about this? Setting the color of the renderer is kind of cheating... but it works...

 
surajeet dev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nate,
Thanks for taking the pain to write the whole code.
but it still doesnt satisfy the requirement.
I WANT THE SELECTED COLOR TO REMAIN EVEN IF I CLICK ON ANY OTHER COMPONENT ON THE CONTAINER.
have u tried clicking on the JTextArea u created?
does the selected color remain?
i dont have JTextArea ,but i have other components,another ComboBox and TextBoxes,whenever i click on them the color disappears and the default color(Foreground--Black
and Background-white)appears.
Regards
Surajeet
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm... if you add another combo box to my example above, the color doesn't change...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic