This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable Color rendering Cell  RSS feed

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)
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!