This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
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
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