Java JTree Expand expandTree(JTree tree, boolean expand)

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

Description

expand Tree

License

Open Source License

Declaration

public static void expandTree(JTree tree, boolean expand) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import java.util.Enumeration;

public class Main {
    public static void expandTree(JTree tree, boolean expand) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }//from  w  w w. j  ava  2 s .  com

    public static void expandTree(JTree tree) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        TreePath treePath = new TreePath(root);
        if (root.getChildCount() > 0) {
            for (Enumeration e = root.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = treePath.pathByAddingChild(n);
                tree.expandPath(path);
            }
        }
    }

    private static void expandAll(JTree tree, TreePath parent,
            boolean expand) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() > 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
}

Related

  1. expandOrCollapseAllRows(final JTree tree, final boolean expand)
  2. expandOrCollapseJTree(JTree tree, boolean expand)
  3. expandPath(JTree tree, TreePath tp)
  4. expandPathOnEdt(final JTree tree, final TreePath path)
  5. expandPaths(JTree tree, Collection paths)
  6. expandTree(JTree tree, TreeNode start, int level)
  7. expandTreeLevels(JTree tree, TreePath parent, boolean expand, int desiredLevel)
  8. expandTreePaths(JTree tree, Enumeration expandPaths)
  9. fullExpand(JTree tree, TreePath parentPath, int nExpansions)