• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issues with graphics being repainted/updated  RSS feed

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this is driving me nuts. I have a table located inside of a scrollpane which is inside of a tabbed pane.

When the contents of table change, I get some varied results on what gets displayed. If the original contents of the pane was only 6 rows long, when it changes it will only show me the first 6 rows (even if there are 7 or more). If I switch tabs and come back the right amount of rows will be shown.

What gives? I've messed around endlessly with repaints and updates to the various pieces. In the same window I have two JLists that don't require any special "treatment" whenever I change the value in the list they update automatically.

I'm using the default table model (vector of vectors for the data, string array for the headings).

Any help, tips, hints are appreciated,
Tad
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I solved the problem by calling:

getViewport().revalidate() on the JScrollPane holding my table.

If anyone has the time/inclination/knowledge I'd love to know why this fixed the problem (and why is it necessary?)

-tad
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look through the DefaultTableModel javadocs. You'll notice there are various methods like removeRow() and insertRow() and setValueAt(). I assume you were calling Vector.changeSomething() instead of DefaultTableModel.changeSomething().

The main difference between calling defaultTableModel.insertRow() and dataVector.add() is that insertRow() will also call fireTableRowsInserted(). If you call the right fireSomethingHappened() methods from AbstractTableModel when the model changes, the view will be updated correctly and you won't need to call revalidate().

If you're not familiar with the Model View Controller pattern, you'll need to look it up before you can really understand Swing. Models allow clients to register listeners that must be notified of all changes. In this case the client is a Swing Look & Feel implementation that wants to draw the table. Since there's no way to automatically detect changes to a Vector, the Look & Feel wasn't able to react to changes in the data.

Calling revalidate() after every change works because it lays out the table as if it's being shown for the first time. This is wasteful because it will cause a lot of layout work that may not be necessary if, for example, the changed part of the model isn't currently shown in the viewport.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that makes sense. Though I really just want to use the table to display some values (no editing) and then when another selection from a list is made show a table associated to that item (a whole new table).

Would it still less wastefull to access the table model?


-Tad
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're replacing the entire table contents I would expect it to be most convenient for you to simply call JTable.setTableModel(new DefaultTableModel(...)). You could also just change the Vector contents and call AbstractTableModel.fireTableDataChanged() to notify the view that all the table data has changed. Reusing the Vector and TableModel is a bit easier on the garbage collector but since objects are only being created as often as the user switches between views there isn't likely to be a performance issue either way.

If there aren't many different tables and you're keeping them in scrollpanes (for example, if you app is a music player and the user selects a playlist to see the list of songs in it) you might even want to consider using a CardLayout with a different JScrollPane and JTable for each table. That way when the user selects a table he has already looked seen and used, it will be scrolled the the same place he left it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!