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 cell color URGENT 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 cell color URGENT" Watch "JTable cell color URGENT" New topic

JTable cell color URGENT

shashikant kulkarni

Joined: Jan 28, 2002
Posts: 3
Hi All
How to change the color of the cell (not the entire row)depending on the value present in it?

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
You can set the color for a single cell by setting a CellRenderer to a column & then check for row & column.
First :
TableColumnModel colmod = table.getColumnModel();
colmod.getColumn(0).setCellRenderer(new ColorRenderer());

class ColorRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable myTable,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(myTable, value, isSelected,
hasFocus, row, column);
if(isSelected == false){
setBackground((((row == 1)) && (column == 0))// particular cell
? (
return this;
You can change row ==1 or column == 1 check by even getting cell value using
if you want to check otherwise.

- Vinod<br />-------<br />SCJP2
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121


Please re-regiester with a user name that follows the JavaRanch Official User Name Policy. You are required to have a user name in the format "First Name" + space + "Last Name".


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: JTable cell color URGENT
It's not a secret anymore!