Java Utililty Methods JTree Path

List of utility methods to do JTree Path

Description

The list of methods to do JTree Path are organized into topic(s).

Method

ListlastPathComponents(TreePath[] paths)
last Path Components
ArrayList lastPathComponents = new ArrayList();
for (int i = 0; i < paths.length; i++) {
    lastPathComponents.add(paths[i].getLastPathComponent());
return lastPathComponents;
voidloadExpansionState(JTree tree, Enumeration expansionState)
Restore the expansion state of a JTree.
if (expansionState != null) {
    while (expansionState.hasMoreElements()) {
        TreePath treePath = (TreePath) expansionState.nextElement();
        tree.expandPath(treePath);
StringmakeLocalFilePath(TreePath treePath)
make Local File Path
String filePath = "";
for (Object node : treePath.getPath()) {
    if (node.toString().equals("/"))
        continue;
    filePath += System.getProperty("file.separator") + node.toString();
return filePath;
TreePathmakeTreePath(TreeNode treeNode)
Creates a tree path from the root node of the tree to the specified treeNode instance
LinkedList list = new LinkedList();
while (treeNode != null) {
    list.addFirst(treeNode);
    treeNode = treeNode.getParent();
Object[] nodes = list.toArray();
return new TreePath(nodes);
TreePathpathByAddingChildAsStr(TreePath currPath, String child)
path By Adding Child As Str
DefaultMutableTreeNode currNode = (DefaultMutableTreeNode) currPath.getLastPathComponent();
for (int i = 0; i < currNode.getChildCount(); i++) {
    if (((DefaultMutableTreeNode) currNode.getChildAt(i)).getUserObject().toString().equals(child)) {
        return new TreePath(((DefaultMutableTreeNode) currNode.getChildAt(i)).getPath());
return null;
booleanpathContains(TreePath[] paths, Class clazz)
path Contains
for (int i = 0; i < paths.length; i++) {
    for (int j = 0; j < paths[i].getPathCount(); j++) {
        if (clazz.isAssignableFrom(paths[i].getPath()[j].getClass())) {
            return true;
return false;
...
TreePathpathToDepth(TreePath path, int depth)
path To Depth
TreePath returnPath = path;
if (path.getPathCount() < depth)
    return new TreePath(new Object[0]);
else {
    for (int i = path.getPathCount(); i > (depth + 1); i--) {
        returnPath = returnPath.getParentPath();
return returnPath;
StringresourceFromTreePath(TreePath path)
resource From Tree Path
StringBuilder resource = new StringBuilder("");
if (path.getPathCount() > 1) {
    for (int i = 2; i < path.getPathCount(); i++) {
        if (i > 2) {
            resource.append('/');
        resource.append(path.getPathComponent(i).toString());
return resource.toString();
voidrestoreSelectionPath(JTree parentTree)
restore Selection Path
if (selectedPath != null) {
    parentTree.setSelectionPath(selectedPath);
TreePathsearchForNodeV2(TreeNode node, String query, JTree tree, TreePath actualPath, boolean isRoot)
search For Node V
if (node.toString().startsWith(query))
    return actualPath.pathByAddingChild(node);
if (node.isLeaf()) {
    return null;
Enumeration<TreeNode> e = (Enumeration<TreeNode>) node.children();
if (!e.hasMoreElements()) {
    TreePath tempPath = actualPath.pathByAddingChild(node);
...