How do you delete the node? Because unless JTree is written badly, if you remove only one node and trigger an event from the model, the tree shouldn't be fully reloaded. For example, with DefaultTreeModel and DefaultMutableTreeNode you should use DefaultTreeModel's removeNodeFromParent method, not DefaultMutableTreeNode's remove, removeAllChildren or removeFromParent methods. If you still want to use those then you need to call the model's nodeXXX methods to trigger the events.
If this still will not work then you first need to collect all expanded paths, then re-expand them afterwards using the methods Maneesh suggested.
I am deleting a node and all of it's children. I wold have to debug thru the code to see if the tree is fully reloaded.
Would you give me a little more guidance/informationon the methods that Maneesh suggested. I am now to Swing but learning fast :-)
Joined: Nov 21, 2008
how can I get all the TreePaths for the visible nodes that are expanded before I delete a Node? If I can do this then I can compare this list to the TreePaths that exist after I delete, use the TreePaths to obtain the Nodes and expand them.
Mi Ku wrote:how can I get all the TreePaths for the visible nodes that are expanded before I delete a Node?
Check out the JTree Javadoc page (click on JTree). getPathForRow, getRowCount() and isExpanded should help you out.
If I can do this then I can compare this list to the TreePaths that exist after I delete, use the TreePaths to obtain the Nodes and expand them.
You can with one if - the tree nodes you are using must be equal. Unfortunately, DefaultMutableTreeNode's equals() method only uses == so if you create completely new tree node instances then you must do the comparison manually. If you reuse the old tree nodes with just a few removed, not a problem.
Joined: Nov 21, 2008
I think you misunderstood. IF I get a List of TreePaths that are expanded before I delelte the Tree Node, then I can get a List of all the TreePaths after I have deleted the tree Node. I can look for the TreePaths in the after list that match the Treepaths in the before List. FOr every TreePath in the after List, I can then use the Treepath to obtain the corresponding Tree Node and expand it.
subject: JTree - keeping expanded nodes expanded after deleting a node