Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox item rendering

 
surajeet dev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this ListRenderer instead...

 
surajeet dev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm... if you add another combo box to my example above, the color doesn't change...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic