This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable cell color URGENT" Watch "JTable cell color URGENT" New topic
Author

JTable cell color URGENT

shashikant kulkarni
Greenhorn

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?

Thanks...
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Hi,
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());
Second:

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
? (Color.blue)
: Color.red));
}
return this;
}
}
.
.
.
You can change row ==1 or column == 1 check by even getting cell value using
myTable.getValueAt(row,column).toString("...")
if you want to check otherwise.
Vinod


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

Joined: Oct 18, 2000
Posts: 4121

shashikant,

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".

Thanks,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable cell color URGENT
 
Similar Threads
Different bg colors for JTable alternate rows - urgent
JTable cell rendering
make uneditable cell a different color
How to give different foreground color for the same cell in JTable?
Color Rows of a Table