• Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternating colors in a JTable

 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • 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.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
 
Mini Pilla
Ranch Hand
Posts: 112
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a really good address. Nice to know. Do you have more of them?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!