JDK-4143848 : JTree: Missing link between TreeNode and TreePath
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1998-05-29
  • Updated: 1998-05-29
  • Resolved: 1998-05-29
Description

Name: el35337			Date: 05/29/98


JTree only provides two methods: expandRow() and expandPath()
to expand tree nodes. However, there is no method to get the
associated TreePath from a TreeNode. 

For instance, if I create a JTree as the following:

      root = new DefaultMutableTreeNode("root");
      child1 = new DefaultMutableTreeNode("child1");
      child2 = new DefaultMutableTreeNode("child2");
      child3 = new DefaultMutableTreeNode("child3");
      grandChild1 = new DefaultMutableTreeNode("grandChild1");
      grandChild2 = new DefaultMutableTreeNode("grandChild2");
      root.add(child1);
      root.add(child2);
      root.add(child3);
      child2.add(grandChild1);
      child3.add(grandChild2);

      DefaultTreeModel treeModel = new DefaultTreeModel(root);

      tree = new JTree(treeModel);

I cannot expand any particular TreeNode in JTree by specifying
the actual TreeNode object because there is no way to find out 
the TreePath of the particular TreeNode among the public 
methods in JTree.

I read the Swing source codes and discovered that the actual
class knowing the TreeNode and the associated TreePath  
is named 'VisibleTreeNode' which provides a method called 
getTreePath(). However, since the VisibleTreeNode is used by
a TreeUI object in JTree, but the TreeUI does not define an 
interface method to access the VisibleTreeNode, therefore, we
cannot find a way to expand a particular tree node by specifying
a TreeNode object in JTree. JTree should provide a method to 
get the TreePath from a TreeNode object, or a method 
named 'expandTreeNode(TreeNode node)'.
(Review ID: 32275)
======================================================================

Comments
EVALUATION JTree is not tied to using TreeNodes. TreeNodes are used by the default implementation of TreeModel. If JTree were to have methods for this it would become tied to only using TreeNodes from the model. If you are using the DefaultTreeModel you can get the path for a TreeNode via: new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot(TreeNode); Or, if you are using DefaultTreeModel and the root has no parents you can just do: new TreePath(defaultMutableTreeNode.getPath()); sky 1998-05-29
11-06-2004

PUBLIC COMMENTS JTree is not tied to using TreeNodes. TreeNodes are used by the default implementation of TreeModel. If JTree were to have methods for this it would become tied to only using TreeNodes from the model. If you are using the DefaultTreeModel you can get the path for a TreeNode via: new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot(TreeNode); Or, if you are using DefaultTreeModel and the root has no parents you can just do: new TreePath(defaultMutableTreeNode.getPath()); sky 1998-05-29
10-06-2004