It seems to me that if your tree contains TreeNodes, it should be trivial
to construct a TreePath for a TreeNode by just calling ".getParent()" on
each node in turn until you reach the root, pushing each node onto the top
of a stack. So it seems almost an oversight to me that TreePath doesn't
have a constructor that takes a single TreeNode, or that any of the known
TreeNode implementations (DefaultMutableTreeNode,
JTree.DynamicUtilTreeNode) includes a method to get a TreePath for that
node.
Oh well, I guess I'll write my own TreeNode implementation that does it.

Signature
Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
"Fly the airplane, then work the problem"
-- Rick Grant (quoting RCAF pilot training)
Thomas Weidenfeller - 26 Sep 2006 17:14 GMT
> It seems to me that if your tree contains TreeNodes, it should be trivial
> to construct a TreePath for a TreeNode by just calling ".getParent()" on
> each node in turn until you reach the root, pushing each node onto the top
> of a stack. So it seems almost an oversight to me that TreePath doesn't
> have a constructor that takes a single TreeNode,
Yes, the JTree-related classes are missing a few things.
> or that any of the known
> TreeNode implementations (DefaultMutableTreeNode,
> JTree.DynamicUtilTreeNode) includes a method to get a TreePath for that
> node.
Well, it is not a TreeNode which is supposed to know a path. However,
the output of DefaultTreeModel.getPathToRoot(TreeNode tn) can be used in
a TreePath constructor. But that is via the model, not directly from a
node, and only if you use (a subclass of) DefaultTreeModel.
/Thomas

Signature
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Steve W. Jackson - 26 Sep 2006 17:24 GMT
> It seems to me that if your tree contains TreeNodes, it should be trivial
> to construct a TreePath for a TreeNode by just calling ".getParent()" on
[quoted text clipped - 6 lines]
>
> Oh well, I guess I'll write my own TreeNode implementation that does it.
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/DefaultMutableT
reeNode.html>
Take another look. It has the getPath method returning an array of
TreeNode, which represents the path from the root node. Since TreePath
has a constructor accepting an array of Object representing exactly that
-- the path from the root to a designated node -- I don't see what's
missing.

Signature
Steve W. Jackson
Montgomery, Alabama
alyawn - 28 Sep 2006 20:53 GMT
Paul,
I think the designers of the API wanted to keep their default tree
model implementation separate from the actual public JTree API.
TreePath belongs to the JTree, TreeModel and TreeModelEvent APIs.
TreeNodes belong to the DefaultTreeModel which is a specific
implementation of the TreeModel API. A little confusing, I know.
I always thought that they should have put the TreeNode,
DefaultMutableTreeNode, etc in a separate (javax.swing.tree.node or
javax.swing.tree.default) package to make life a little easier for
developers who are new to the API. But as other posters have mentioned,
there is an easy way to get at a node's path through the
DefaultTreeModel. And maybe you should submit an RFE for a
TreeNode.getPath() addition to the API. As you mentioned, the code for
that is dirt simple.
-Alan
-http://everydaycoder.com/