• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable TableModel and TableColumnModel

 
Russell Wurth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My MVC design for my GUI has a TableModel that is updated with the correct number of rows to display depending on the Search/Search All results.

My problem is that I have a TableColumnModel instance variable as part of the main gui. In the constructor of my main gui, I set my TableColumnModel variable in the following fashion:


Now, using setAutoCreateColumnsFromModel() seems to be the only way to prevent my mainTable from losing it's columnModel settings since they are recreated every time I re-render the table using mainTable.setModel().

It works, but is there a better way, such as tying my column formatting in my TableModel? I could also create a new class MyTableColumnModel and call mainTable.setColumnModel() after every setModel() call, but that seems like overkill since I am not formatting my columns that much. I could also alter my method that performs the mainTable.setModel() call to format the columns every time, but again, seems like more work than needed.

Thoughts?
 
mike acre
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite what Max has to say on the subject, I wouldn't keep replacing the TableModel. Have a setData method in your TableModel and fire a tableDataChanged. Then the link with the view will be maintained and decoupling the view from the controller will be easier.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a setData method in your TableModel and fire a tableDataChanged.
This is similar to the way I am planning on handling updates in my JTable. I have implemented this in past applications, and haven't had any problems.
 
Russell Wurth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Would it be something like this for every time I need to update the display?


Thanks in advance!
 
mike acre
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you subclass AbstractTableModel you get event firing methods for free, no need to create your own events.

When you do myJTable.setModel(myModel), do this just the once, this sets internal listeners between the two. Then in your Table models setData method, do a fireTableDataChanged();
 
Russell Wurth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great idea Mike, it works! The design is much more straight forward than regenerating the TableModel in the Controller.

One question though: With Max's design, the setupTable method was able to capture and reset the selected row between data refreshes.

I am guessing the only way I could do this is if I subclass my JTable to override it's tableChanged() method, capture the selected index, refresh the data, and reselect.

Russell
 
mike acre
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Russell Wurth:


I am guessing the only way I could do this is if I subclass my JTable to override it's tableChanged() method, capture the selected index, refresh the data, and reselect.

Russell


Absolutely not!
You just register a ListSelectionListener on your table and keep track of the last selection, then store this again before a search and reselect it after a search.
 
Russell Wurth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Thanks, I achieved this result after I had posted. I wasn't thinking clearly last nite...

Thanks again

Russell Wurth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic