Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree - keeping expanded nodes expanded after deleting a node

 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you delete a node from a JTree, Is there a way to keep all the nodes expanded that were expanded before the delete?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JTree#makeVisible(TreePath path) and JTree#setExpandedState()
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)

Thank you
 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mi Ku
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic