• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JTable cell color URGENT  RSS feed

 
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...
 
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
 
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
Boost this thread!