Java JTree Expand expandPaths(JTree tree, Collection paths)

Here you can find the source of expandPaths(JTree tree, Collection paths)

Description

expand Paths

License

Open Source License

Declaration

static public void expandPaths(JTree tree, Collection<TreePath> paths) 

Method Source Code


//package com.java2s;
import java.util.Collection;

import java.util.Iterator;

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

public class Main {
    static public void expandPaths(JTree tree, Collection<TreePath> paths) {
        if (paths != null) {
            for (Iterator<TreePath> it = paths.iterator(); it.hasNext();) {
                TreePath tp = it.next();

                expandPath(tree, tp);//from ww w  .jav a 2s . c  o m
            }
        }
    }

    static public void expandPath(JTree tree, TreePath tp) {
        Object root = tree.getModel().getRoot();
        expandPath(tree, new TreePath(root), tp, 0);
    }

    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. expandNodesAtLevel(int level, JTree tree, TreePath parent)
  2. expandOrCollapseAllRows(final JTree tree, final boolean expand)
  3. expandOrCollapseJTree(JTree tree, boolean expand)
  4. expandPath(JTree tree, TreePath tp)
  5. expandPathOnEdt(final JTree tree, final TreePath path)
  6. expandTree(JTree tree, boolean expand)
  7. expandTree(JTree tree, TreeNode start, int level)
  8. expandTreeLevels(JTree tree, TreePath parent, boolean expand, int desiredLevel)
  9. expandTreePaths(JTree tree, Enumeration expandPaths)