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);

}

}

[2233 byte] By [Ian_Beaumonta] at [2007-11-27 9:38:54]
# 1

> So this is my current approach (below), is that the best way?

Its fine, except:

a) you should invoke super.getTableCellRendererComponent as the first line of your code.

b) no need to cast the TableModel to an AbstractTableModel. You should be programming to Interfaces, not implementations.

camickra at 2007-7-12 23:13:08 > top of Java-index,Desktop,Core GUI APIs...