Here you can find the source of buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)
public static TreePath buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)
//package com.java2s; //License from project: Open Source License import java.util.Enumeration; import javax.swing.JTree; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Main { public static TreePath buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable) { TreeNode node = (TreeNode) parent.getLastPathComponent(); String o = node.toString(); // If equal, go down the branch if (o.equals(nodes[startdepth])) { // If at end, return match if (startdepth == nodes.length - 1) { return parent; }/*from w w w .j a va2 s . c o m*/ // Traverse children if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements();) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); if (n.isLeaf() && expandable) { return parent; } TreePath result = buildTreePath(tree, path, nodes, startdepth + 1, expandable); // Found a match if (result != null) { return result; } } } } // No match at this branch return null; } }