Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Most Popular :-) JTree refresh problem

843804Jul 30 2001 — edited Jan 30 2010
Hello everyone,

I am posting about a JTree refresh problem that seems to be very popular....
My code uses a custom model that implements TreeModel (unfortunately I cannot just inherit from DefaultTreeModel). My custom model also provides the same implementations for the fireTreeNodesXXXX() and fireTreeStructureChanged() methods as javax.swing.tree.DefaultTreeModel.

When nodes are dynamically inserted into the model, I attempt to update the tree UI to reflect the new changes.

All approaches except invoking updateUI() fail!
The problem with using updateUI() is that for starters it is meant to be used to notify the control that the L&F has changed. Using updateUI will surely force the tree to redraw, with serious performance penalties. In addition if you want custom height for each rown in the tree, update UI will force a call to setRowHeight() on the table, effectively effectively resetting the tree to fixed row height rendering!

According to all JTree documentation (and Swing use examples provided by SUN and others), invoking the fireTreeNodeInserted() and fireTreeStructureChange() events should be sufficient since all observers (including the JTree instance) are notified of model changes. I have verified that the JTree is actually notified of the structure change event.

Using calls to fireTreeNodeInserted() and fireTreeStructureChange() after the model has changed, does not update the tree UI for me. I have also tried update(), invalidate(), revalidate(), repaint(), with no effect.

Since many people keep posting about this problem over and over again, either there is a lack of proper documentation on this topic or there are some "special features" in the JTree implementation the public is not aware of.
Perhaps some Guru from SUN would be kind enough to help us in this thread, and provide a clear explanation (perhaps some examples) of how this JTree update should be done, both within a UI thread and from an non-UI worker thread!

As always everyone's contribution is appreciated.
Thanks in advance...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 27 2010
Added on Jul 30 2001
18 comments
1,824 views