Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Alternating colors in a JTable

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
I was wondering how i can make every other row in my table a different color?
If possible i would like to include the colors in my table model.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is possible. I did this in a project.
You have to do this in your own TableCellRenderer.
There you have to implement the function getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column).
The value is exactly the value, which is returned by your TableModel. So you can cast it to your own Object, let's say it is called ColorValue.
Then you ask your ColorValue for the color.
If your TableCellRenderer extends from DefaultTableCellRenderer then it also inherits the methods of JLabel, so you can set foreground and background colors.
Bye
Rene
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this out
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a really good address. Nice to know. Do you have more of them?
 
reply
    Bookmark Topic Watch Topic
  • New Topic