Here you can find the source of expandOrCollapseAllRows(final JTree tree, final boolean expand)
tree
.
Parameter | Description |
---|---|
tree | The tree to expand or collapse. |
expand | If <code>true</code>, then the given <code>tree</code> will be expanded. |
Parameter | Description |
---|---|
IllegalArgumentException | If <code>tree == null</code>. |
public static final void expandOrCollapseAllRows(final JTree tree, final boolean expand) throws IllegalArgumentException
//package com.java2s; /**//from w w w . j a va 2 s.c o m * Copyright (C) 2013, University of Manchester and University of Southampton * * Licensed under the GNU Lesser General Public License v2.1 * See the "LICENSE" file that is distributed with the source code for license terms. */ import java.util.Enumeration; import javax.swing.JTree; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Main { /** * Expand or collapse every node in the given <code>tree</code>. * * @param tree The tree to expand or collapse. * @param expand If <code>true</code>, then the given <code>tree</code> will be expanded. * @throws IllegalArgumentException If <code>tree == null</code>. */ public static final void expandOrCollapseAllRows(final JTree tree, final boolean expand) throws IllegalArgumentException { if (tree == null) { throw new IllegalArgumentException(new NullPointerException("tree")); } expandOrCollapseAllRows(tree, new TreePath(tree.getModel().getRoot()), expand); } /** * Expand or collapse nodes in the given <code>tree</code> that match the given <code>path</code>. * * @param tree The tree to expand or collapse. * @param path The tree path. * @param expand If <code>true</code>, then the given <code>tree</code> will be expanded. * @throws IllegalArgumentException If <code>tree == null || path == null</code>. */ private static final void expandOrCollapseAllRows(final JTree tree, final TreePath path, final boolean expand) throws IllegalArgumentException { if (tree == null) { throw new IllegalArgumentException(new NullPointerException("tree")); } else if (path == null) { throw new IllegalArgumentException(new NullPointerException("path")); } // Get the last component of the given 'path' (should be a tree node.) final TreeNode node = (TreeNode) path.getLastPathComponent(); // Call this method for each child node. if (node.getChildCount() > 0) { @SuppressWarnings("unchecked") final Enumeration<TreeNode> e = node.children(); while (e.hasMoreElements()) { expandOrCollapseAllRows(tree, path.pathByAddingChild(e.nextElement()), expand); } } // Expand or collapse the given 'tree'. if (expand) { tree.expandPath(path); } else { tree.collapsePath(path); } } }