• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Color Change

 
Prakash Radhakrishan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. How do I put different colours in different rows like first row red color, second row blue and so on....and how to do the same thing but with individual cells of a JTable.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi prakash,
to play with colours u need to add a renderer to ur table. the code goes like this.
JTable myTable = new JTable(.....);
first of all u need to write a renderer class example is
class gridTableCellRenderer extends JLabel implements TableCellRenderer{
/// inside getTableCellRendererComponent function write all
/// the color things u want
public Component getTableCellRendererComponent( JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column )
{
if(row == 2)
setBackground(Color.green);
else
{
if(row == 4 && column == 1)
setBackground(Color.red);
}
}
}
////add renderer for every type of colunms
//// that is if u have one column of type JLable, & other of type
/// Integer then u'll write
myTable.setDefaultRenderer(JLabel.class,new gridTableCellRenderer());

hope this helps
deekasha
 
Prakash Radhakrishan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi deekasha gunwant,
Thank You for the response. It works fine.
prakash,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic