Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Color rendering Cell

 
fahad siddiqui
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
rowTemp=row;
cell= super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row-1, column);

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

if (!CriteriaVO.getAnotherStartStringExpression().equals("")) {
if(value[3].toString().length()>=CriteriaVO.getAnotherStartStringExpression().length()){
for (int i = 0; i < (value[3].toString().length()
- CriteriaVO.getAnotherStartStringExpression().length()); i++) {
containStartString = value[3].toString().substring(i,
i+CriteriaVO.getAnotherStartStringExpression().length());
if (containStartString.equals(CriteriaVO
.getAnotherStartStringExpression())) {
colorToSetRequest=true;
break;
}
}
if(colorToSetRequest){
cell.setBackground(Color.pink);
colorToSetRequest=false;
return cell;
}
else
cell.setBackground(Color.white);
}
}

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic