my dog learned polymorphism*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Color rendering Cell" Watch "JTable Color rendering Cell" New topic
Author

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)
{
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.
 
 
subject: JTable Color rendering Cell
 
Similar Threads
JTable Cell Renderer
JTable renderer - Change the backColor of 2 specific rows.
Color in JTable
Changing background color of a JTable row.
JTable cell color URGENT