Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save a Selected TreePath and reuse later

 
Jon Wynett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic