File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

JTable Color Change

Prakash Radhakrishan

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)
if(row == 4 && column == 1)
////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
Prakash Radhakrishan

Joined: Jun 27, 2000
Posts: 3
Hi deekasha gunwant,
Thank You for the response. It works fine.
I agree. Here's the link:
subject: JTable Color Change
It's not a secret anymore!