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?
TableModel is only there for storing the data and informing the views (JTables mostly) about any updates. TableColumnModel is used to determine which of the columns defined in the TableModel will actually be shown in the view. As such, you don't need to update your TableModel at all, just the TableColumnModel. And you can do that through the JTable itself. Add columns using addColumn, remove them using removeColumn, retrieve them using getColumnModel().getColumn.
So there is no need to create any custom classes. Hope that is clearer ;)
Joined: May 29, 2005
Rob Prime wrote:TableModel is only there for storing the data and informing the views (JTables mostly) about any updates. TableColumnModel is used to determine which of the columns defined in the TableModel will actually be shown in the view. As such, you don't need to update your TableModel at all, just the TableColumnModel. And you can do that through the JTable itself. Add columns using addColumn, remove them using removeColumn, retrieve them using getColumnModel().getColumn.
This is good data, thanks -- I wish there was more explanation of the purposes of classes like this.
So I (currently) have 6 possible columns of data for my table; I want my user to be able to pick whether 3 of them appear at all, and which of the remaining two to display. They involve custom renderers and fixed (default) widths (I let the user resize, but they default to one width and stay there regardless of window resizing; only the last column varies; I've got that part working).
If I understand you, I can do all of this from the class that has my JTable reference. My question is, is it appropriate to do it in a class that extends TableColumnModel?
I was trying to put all the following into a class: data about what columns might be displayed (their width and renderer, for instance); whether they were currently displayed; information about the UI used to change columns, and response to some message/notice/event that says it is time to figure out what new column arrangement is to be displayed.
Does it fit these classes to do that? Do I extend TableColumnModel to do that, or create a different class that uses a reference to JTable to do the add/remove/etc. to columns as you mention above?
Joined: Jun 13, 2009
I was trying to put all the following into a class: ...
That is the purpose of the TableColumn class. It contains information like. the width, renderer, editor, heading and which column in the TableModel has the data.
The TableColumnModel then just controls which TableColumns are visible on the table and the order in which the columns are displayed. As the user changes the widths of each column the TableColumn is updated. If the user reorders the columns then the TableColumnModel is updated.