I am working with netbeans IDE7.4, I am adding rows to the JTable
at run-time and now I want to set background color for a particular row.
Now the problem is that when the value of that row is changed the color of that particular row is not changed and when I scroll up or down the table the changes are applied.
How to refresh the table at run-time? How to set background color of particular row at runtime?
This is renderer class am using for coloring particular row:
public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer
{
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
final java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Object val=table.getValueAt(row, 2);
String sval=val.toString();
sval=sval.replaceAll(":","");
int ival=Integer.parseInt(sval);
if(ival==0)
{
cellComponent.setForeground(Color.black);
cellComponent.setBackground(Color.red);
}
else
{
cellComponent.setBackground(Color.white);
cellComponent.setForeground(Color.black);
}
if (isSelected)
{
cellComponent.setForeground(table.getSelectionForeground()); cellComponent.setBackground(table.getSelectionBackground());
}
return cellComponent;
}
}
and am assigning to jtable like this :
newViewTable.setDefaultRenderer(Object.class,new MyCellRenderer());
newViewTable is the name of JTable.