Java JTree Expand expandOrCollapseJTree(JTree tree, boolean expand)

Here you can find the source of expandOrCollapseJTree(JTree tree, boolean expand)

Description

Expands a JTree completely

License

Open Source License

Parameter

Parameter Description
tree a parameter
expand true means expand, false collapse

Declaration

public static void expandOrCollapseJTree(JTree tree, boolean expand) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import javax.swing.JTree;

import javax.swing.tree.TreePath;

public class Main {
    /**//from w ww.  java  2 s  .  c om
     * Expands a JTree completely
     * @param tree
     * @param expand true means expand, false collapse
     */
    public static void expandOrCollapseJTree(JTree tree, boolean expand) {
        Object root = tree.getModel().getRoot();

        // Traverse tree from root
        expandOrCollapseJTree(tree, new TreePath(root), expand);
    }

    /**
     * Expands a JTree from a given path.
     * @param tree
     * @param parent
     * @param expand
     */
    public static void expandOrCollapseJTree(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        Object node = parent.getLastPathComponent();
        for (int i = 0; i < tree.getModel().getChildCount(node); i++) {
            Object n = tree.getModel().getChild(node, i);
            TreePath path = parent.pathByAddingChild(n);
            expandOrCollapseJTree(tree, path, expand);
        }

        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
}

Related

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