*
The moose likes Swing / AWT / SWT and the fly likes Setting color in a JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting color in a JComboBox" Watch "Setting color in a JComboBox" New topic
Author

Setting color in a JComboBox

Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hello every one,
I am trying to set color in a JComboBox the code is as follows. But There is a poblem the color list is not visible.


Suman Mummaneni
Bangalore
India
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hi Craig Wood.
I have tried out your code. But the problem is, when I have set the color in the combo box and I move on to the next element in the dialog. The color that has been set in the combo box go and the text select color appears there.
I have use your renderer in the follwing just try running it.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hi Craig,

thank you. It works now. Can you tell me why you have overridden the painComponen method in JComponent. I am just eager to know what went wrong with my code.
[ April 11, 2006: Message edited by: Suman Mummaneni ]
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Originally posted by Suman Mummaneni:
Hi Craig,

thank you. It works now. Can you tell me why you have overridden the painComponen method in JComponent. I am just eager to know what went wrong with my code.

[ April 11, 2006: Message edited by: Suman Mummaneni ]


Should we do this overriding of paintComponent in the renderer for JTable if we want to have colours in the cells of the JTable.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I couldn't find the way to break into the plaf (look and feel) code to set the value in the JComboBox. Thought I had it the first time but didn't check focus-lost behavior. So I gave up on the plaf idea and tried a simpler approach: just paint the color for each JLabel.
For JTables I think the original idea you had will work better. The difficulty with the JComboBox was in the difference between the JList (which uses the custom renderer) and the comboBox value (rendered in plaf code). We don't have this added complexity in JTable; everything is showing. You do have to build in enough state in the renderer class to be able to identify the row, column for the cell colors.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting color in a JComboBox
 
Similar Threads
BoxLayout alignment issue
2D Array Help
ActionListener problems
JTabbedPane problem
panels not displayed fullly in Jframe