calling reload method on DefaultTreeModel without closing nodes
Kevin Tysen
Ranch Hand
Joined: Oct 12, 2005
Posts: 247
posted
0
Calling reload() on a DefaultTreeModel object seems to be automatically causing all the nodes in the JTree to be hidden, except for the nodes which are direct descendants of the root node.
My program involves removing nodes and adding nodes to the node structure in the DefaultTreeModel, but the changes in the node structure were not displayed correctly until I added the call to the reload method. But then all the nodes which were open close up automatically. I want a good way to show changes to the node structure without nodes closing.
You have to call super.fireTreeNodesInserted or super.fireTreeNodesRemoved when you add/remove nodes. This call will leave the opened/closed state of the nodes as it is (and also the current selection as it is).
Call should be like:
where ROOT is the tree root,
parent1, parent2 is the hierarchy
addedIndex is the index of where the child is added to the parent (in this case 'parent2')
and finally the added child object itself.
Same applies for removal. You have to pass the path, removed index and the actual object being removed.
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
subject: calling reload method on DefaultTreeModel without closing nodes