• 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

Setting color in a JComboBox

 
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic