Java JTree Expand buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)

Here you can find the source of buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)

Description

build Tree Path

License

Open Source License

Declaration

public static TreePath buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth,
            boolean expandable) 

Method Source Code


//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;
    }
}

Related

  1. autoExpand(JTree tree, TreePath path, int maxLines, int maxChildToExpand, boolean dontExpandToLeafs)
  2. expand(JTree tree, int depth)
  3. expand(JTree tree, int level)
  4. expand(JTree tree, int startingIndex, int rowCount)
  5. expandAll(final JTree tree, final TreePath parent, final boolean expand)