I have created one Jtable.This table consist of two columns name and timestamp. I want to make color of row yellow if name is "jane". Following is the code for that :-
class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
String name = table.getModel().getValueAt(row, 0).toString();
if (name.trim().equals("jane")) {
c.setBackground(Color.YELLOW);
}
return c;
}
}
However instead of changing color of row to yellow for particular name, it is changing color of every row.I am setting data of table as following
tableModelName = (DefaultTableModel)jTableName.getModel();
jTableName.setDefaultRenderer(Object.class,new CustomRenderer());
for(int i=0; i<records.size(); i++)
{
tableModelName.addRow(records.get(i));
}
What I am doing wrong ?
table.convertRowIndexToModel()
andtable.convertColumnIndexToModel()
before accessing the model. Else, any table sorting or column reordering will make your code fail. – Bolide