• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

custom table model and column model for JTable

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But removing columns from the TableModel means throwing that data away. Removing columns from the TableColumnModel means only hiding that data.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic