Java JTree Expand expandPath(JTree tree, TreePath tp)

Here you can find the source of expandPath(JTree tree, TreePath tp)

Description

expand Path

License

Open Source License

Declaration

static public void expandPath(JTree tree, TreePath tp) 

Method Source Code

//package com.java2s;

import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Main {
    static public void expandPath(JTree tree, TreePath tp) {
        Object root = tree.getModel().getRoot();
        expandPath(tree, new TreePath(root), tp, 0);
    }/*from w ww  . j ava  2  s.co  m*/

    static public void expandPath(JTree tree, TreePath targetPath, TreePath tp, int pos) {
        Object[] nodes = tp.getPath();

        Object node = targetPath.getLastPathComponent();
        Object tpNode = nodes[pos];

        if (node.equals(tpNode)) {
            tree.expandPath(targetPath);
        } else {
            return;
        }

        TreeModel model = tree.getModel();
        if (pos < nodes.length - 1) {
            int n = model.getChildCount(node);
            for (int i = 0; i < n; i++) {
                Object child = model.getChild(node, i);
                if (child.equals(nodes[pos + 1])) {
                    expandPath(tree, targetPath.pathByAddingChild(child), tp, pos + 1);
                }
            }
        }
    }
}

Related

  1. expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)
  2. expandLevels(JTree tree, int levels, boolean expand)
  3. expandNodesAtLevel(int level, JTree tree, TreePath parent)
  4. expandOrCollapseAllRows(final JTree tree, final boolean expand)
  5. expandOrCollapseJTree(JTree tree, boolean expand)
  6. expandPathOnEdt(final JTree tree, final TreePath path)
  7. expandPaths(JTree tree, Collection paths)
  8. expandTree(JTree tree, boolean expand)
  9. expandTree(JTree tree, TreeNode start, int level)