This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JComboBox item rendering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox item rendering" Watch "JComboBox item rendering" New topic
Author

JComboBox item rendering

surajeet dev
Greenhorn

Joined: Jun 19, 2002
Posts: 3
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

Joined: Oct 18, 2000
Posts: 4121

Use this ListRenderer instead...



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
surajeet dev
Greenhorn

Joined: Jun 19, 2002
Posts: 3
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

Joined: Oct 18, 2000
Posts: 4121

Then what about this? Setting the color of the renderer is kind of cheating... but it works...

surajeet dev
Greenhorn

Joined: Jun 19, 2002
Posts: 3
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

Joined: Oct 18, 2000
Posts: 4121

Ummm... if you add another combo box to my example above, the color doesn't change...
 
 
subject: JComboBox item rendering
 
Similar Threads
JComboBox: Opaque problem
problem selecting JList item
Checkbox items incombobox rendering
JComboBox and "long" items rendered via HTML
Change the font in a JList