Here you can find the source of getPath(TreeNode treeNode)
public static TreePath getPath(TreeNode treeNode)
//package com.java2s; import java.util.ArrayList; import java.util.List; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Main { public static TreePath getPath(TreeNode treeNode) { List<TreeNode> nodes = new ArrayList<TreeNode>(); while (treeNode != null) { nodes.add(0, treeNode);/*from w w w . j a v a 2s.com*/ treeNode = treeNode.getParent(); } return nodes.isEmpty() ? null : new TreePath(nodes.toArray()); } }