aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTree - keeping expanded nodes expanded after deleting a node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree - keeping expanded nodes expanded after deleting a node" Watch "JTree - keeping expanded nodes expanded after deleting a node" New topic
Author

JTree - keeping expanded nodes expanded after deleting a node

Mi Ku
Greenhorn

Joined: Nov 21, 2008
Posts: 11
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

Check out JTree#makeVisible(TreePath path) and JTree#setExpandedState()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mi Ku
Greenhorn

Joined: Nov 21, 2008
Posts: 11
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

Joined: Nov 21, 2008
Posts: 11
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

Joined: Oct 27, 2005
Posts: 19791
    
  20

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

Joined: Nov 21, 2008
Posts: 11
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree - keeping expanded nodes expanded after deleting a node