Lost CellRenderer when updating TableModel

I am working on some code, where I need to modify the displayed columns in a table.

The table consists of cells that uses a custom CellRenderer implemented by the class MonitorCellRenderer.

So far I have some of the functionality working in that I can make the table display less columns. This is achieved by modifying the data stored in the TableModel. In fact what I do is re-create the Column Headers and data.

This is all achieved by firing a fireTableStructureChanged() event. Then I recompose the data in the Table Model. I call JTable.setModel() with the Table Model reference again, and the table is re-structured. BUT....

The cell rendering does not work, all that is displayed is the object reference for the data (The data for each cell is stored in an object). This is the same as would have happened if I had not initialised a CellRenderer for the table, so I implemented the following code in my TableModelListener

for(int col = 0; col < monitorTable.getModel().getColumnCount(); col++)

{

column = monitorTable.getColumnModel().getColumn(col);

column.setCellRenderer(new MonitorCellRenderer());

column.setPreferredWidth(100);

column.setResizable(false);

}

monitorTable is the JTable. This code is cribbed from the inital set up for the table, so is essentially what I want. What I have found is that these settings for the column are ignored, because if I change the preferred width, it does not change.

I'm thinking that somehow my table ColumnModel reference is wrong, because it is referencing something else.

Can anyone suggest what may be going wrong.

Thanks

[1867 byte] By [Stevefkia] at [2007-11-27 10:24:27]
# 1

One way I never have to deal with missing references for the CellRenderer is to override the getCellRenderer( ) method of the JTable to return the desired renderer where requried.

final TableCellRenderer renderer = new SomeRenderer();

JTable table = new JTable() {

public TableCellRenderer getCellRenderer(int row, int col) {

return renderer;

}

};

Dont know if this will help in your case, but it may be worth a try. This way, the renderer addition is independent of any of the models, column or table.

ICE

icewalker2ga at 2007-7-28 17:28:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

> This is all achieved by firing a fireTableStructureChanged() event.

This causes the TableColumnModel to be recreated from scratch, therefore you lose renderers and editors that have manually been assigned to the table.

> So far I have some of the functionality working in that I can make the

> table display less columns. This is achieved by modifying the data

> stored in the TableModel. In fact what I do is re-create the Column Headers and data.

Sounds unecessary to me. Just remove the TableColumn from the TableColumnModel and the table will be painted correctly. There is no need to play with the data in the TableModel.

camickra at 2007-7-28 17:28:02 > top of Java-index,Desktop,Core GUI APIs...