Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes How to save a Selected TreePath and reuse later Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to save a Selected TreePath and reuse later" Watch "How to save a Selected TreePath and reuse later" New topic
Author

How to save a Selected TreePath and reuse later

Jon Wynett
Greenhorn

Joined: Jun 28, 2004
Posts: 10
What I would like to do is save a SelectedPath from a JTree and after the tree is closed, at some later time, automatically expand the same path in a new tree. The new tree will probably have the same structure but not definitely and that is why I will have a new tree.

I thought this would be easy and I started by saving the TreePath retrieved from getSelectedPath. Later, I figured I could use it to do an expandPath() in the new tree. However that doesn't work. The new tree will not visibly expand to a path or nodes retrieved from another tree even if the node values are the same. The tree will act as if the new path is expanded but it will not become visible on the screen.

What I found does work is to find the same nodes in the current tree, build a TreePath from that and tell it to expand. However this takes a lot of work and there has just got to be an easier way.

Thanks,

Jon
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
I don't think so. You're trying to expand a path of nodes that do not exist in your tree ! You could try subclassing JTree to change the behaviour of expandPath(). I wouldn't recommend it though. I suggest two things :

1. Keep the pain. Use your method that finds an equivalent path in a tree based on node value, stick it in a utility class and continue.
OR
2. When you update the data in your table. Do not create new nodes. Use DefaultMutableTreeNode and call setUserObject(). This way expandPath will work (even if the values in the path have changed).

hth, D.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save a Selected TreePath and reuse later