aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes custom table model and column model for JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "custom table model and column model for JTable" Watch "custom table model and column model for JTable" New topic
Author

custom table model and column model for JTable

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Do I need a custom TableColumnModel class?


The DefaultTableModel supports dynamic adding/removing columns from the TableModel itself. In this cause you add/remove the data as well.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

But removing columns from the TableModel means throwing that data away. Removing columns from the TableColumnModel means only hiding that data.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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 ;)
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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?

rc
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: custom table model and column model for JTable