I have an extension of AbstractTableModel that I built partially from examples at
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data ; it contains a collection of columns which I initialize at instantiation.
Now I want to alter my code so that the number and content of columns can vary according to user selection, so I need to be able to add and remove columns from the display.
My AbstractTableModel has its own additional method, resetTableColumns, that goes through my list of objects representing columns; it takes a parameter of type TableColumnModel, because it needs to obtain each TableColumn in the TableColumnModel and set its width and renderer. In my only call to this method so far, I obtain the TableColumnModel from the JTable.
My current working code instantiates my table model, then adds objects describing each column to the table model, then instantiates the JTable, passing in my custom AbstractTableModel. My problem is that the column information seems to have to exist in the AbstractTableModel at the time the JTable is instantiated, or my method to reset the columns does not work.
I have tried putting my column data into my AbstractTableModel and then calling JTable.setModel() , but that doesn't work: when I try to iterate through the columns in the TableColumnModel after doing that, I get an error indicating that my TableColumnModel doesn't have any columns in it.
On looking at it, it does seem odd that I'm creating a collection of columns in the TableColumnModel when there is a separate ColumnModel object; perhaps I am confused over just what these two things represent.
Can someone please point me to (preferably) an
explanation of the concepts behind TableModel and TableColumnModel and related classes, or at least an example that adds and removes columns at runtime, including setting attributes like width and renderer?
Do I need a custom TableColumnModel class? How do I split the responsibilites of my custom TableColumnModel and my custom AbstractTableModel?
rc