Java JTree Path getPath(TreeNode treeNode)

Here you can find the source of getPath(TreeNode treeNode)

Description

Given a TreeNode get the corresponding TreePath

License

BSD License

Parameter

Parameter Description
treeNode a parameter

Declaration

public static TreePath getPath(TreeNode treeNode) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

import java.util.ArrayList;

import java.util.List;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
    /**//from w  w  w .  jav  a2 s .  com
     * Given a <code>TreeNode</code> get the corresponding <code>TreePath</code>
     * @param treeNode
     * @return 
     */
    public static TreePath getPath(TreeNode treeNode) {
        List<Object> nodes = new ArrayList<Object>();
        if (treeNode != null) {
            nodes.add(treeNode);
            treeNode = treeNode.getParent();
            while (treeNode != null) {
                nodes.add(0, treeNode);
                treeNode = treeNode.getParent();
            }
        }

        return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
    }
}

Related

  1. findV2MetaTreeNode( String nodeXmlPath)
  2. getDepth(JTree tree)
  3. getExpatnedTreePaths(JTree tree)
  4. getObjectPathForNode(final TreeNode node)
  5. getPath(TreeNode node)
  6. getPath(TreeNode treeNode)
  7. getPath(TreeNode treeNode)
  8. getPath(TreeNode treeNode)
  9. getPathStr(TreeNode[] projectNodes)