JTable - making a cells icon dependant on another cells value
I need to make the icon displayed in a row dependant on the value of a different cell in that row. The only way I can see of getting hold of the value of a different cell in a TableCellRender is to override getTableCellRendererComponent and then get the table model.
So this is my current approach (below), is that the best way?
TIA
publicclass TestTableCellRendererextends DefaultTableCellRenderer{
ImageIcon icon1;
ImageIcon icon2;
public TestTableCellRenderer(){
// Load icons
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL= cldr.getResource("icon1.png");
icon1 =new ImageIcon(imageURL);
imageURL= cldr.getResource("icon2.png");
icon2 =new ImageIcon(imageURL);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
AbstractTableModel tableModel = (AbstractTableModel)table.getModel();
String status =(String) tableModel.getValueAt(row, 3);
if (status.equals("Status1")){
setIcon(icon1);
}elseif (status.equals("Status2")){
setIcon(icon2);
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}

