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?
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.
But removing columns from the TableModel means throwing that data away. Removing columns from the TableColumnModel means only hiding that data.
Joined: Jun 13, 2009
Rob Prime wrote:But removing columns from the TableModel means throwing that data away. Removing columns from the TableColumnModel means only hiding that data.
Yes, I mentioned that. I was answering the question if a custom model needed to be created.
The point I was trying to make is that you don't need to create a custom TableModel. You can use the "addColumn" to add columns of data dynamically.
Then if you want to hide columns you remove the TableColumn from the TableColumnModel as you had suggested.
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: custom table model and column model for JTable