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 rendering Cell 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 rendering Cell" Watch "JTable Color rendering Cell" New topic

JTable Color rendering Cell

fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Posts: 85
I have a JTable on which i keep adding rows. i want to change the color of the rows according to the data being put into it. i haveused the DefaultTableCell Renderer as follows:

public class CustomTableCellRenderer extends DefaultTableCellRenderer

private int count=2;
String containStartString = "";
String containEndString = "";
boolean colorToSetRequest=false;
boolean colorToSetResponse=false;
Component cell;
int rowTemp=0;

public Component getTableCellRendererComponent
(JTable table,Object value, boolean isSelected,
boolean hasFocus, int row, int column)
cell= super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row-1, column);

System.out.println("this is value "+value.length);
if(row%2 ==0) {
} else {

if (!CriteriaVO.getAnotherStartStringExpression().equals("")) {
for (int i = 0; i < (value[3].toString().length()
- CriteriaVO.getAnotherStartStringExpression().length()); i++) {
containStartString = value[3].toString().substring(i,
if (containStartString.equals(CriteriaVO
.getAnotherStartStringExpression())) {
return cell;

return cell;


So, what happens is i add first line of data and condition is satisfied and color is changed ... then when next line is put, it satisfies another data and so color for that row must change ..but the color of all the rows in the table change to the new color.
But when i write the if and else condition written in the start for row%2, it gives a perfect result of alternating colors..

Please advise.
I agree. Here's the link:
subject: JTable Color rendering Cell
It's not a secret anymore!