GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable Color Change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Color Change" Watch "JTable Color Change" New topic
Author

JTable Color Change

Prakash Radhakrishan
Greenhorn

Joined: Jun 27, 2000
Posts: 3
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

Joined: May 06, 2000
Posts: 396
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

Joined: Jun 27, 2000
Posts: 3
Hi deekasha gunwant,
Thank You for the response. It works fine.
prakash,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable Color Change