• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable cell color URGENT

 
shashikant kulkarni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic