Java JTree Expand expandAll(final JTree tree, final TreePath parent, final boolean expand)

Here you can find the source of expandAll(final JTree tree, final TreePath parent, final boolean expand)

Description

expand All

License

Open Source License

Declaration

private static void expandAll(final JTree tree, final TreePath parent, final boolean expand) 

Method Source Code

//package com.java2s;
/*//from ww  w. j  a v  a  2  s.  c o  m
Copyright 2015 Google Inc. All Rights Reserved.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.util.Enumeration;

import javax.swing.JTree;

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

public class Main {
    private static void expandAll(final JTree tree, final TreePath parent, final boolean expand) {
        // Traverse children
        final TreeNode node = (TreeNode) parent.getLastPathComponent();

        if (node.getChildCount() >= 0) {
            for (final Enumeration<?> e = node.children(); e.hasMoreElements();) {
                final TreeNode n = (TreeNode) e.nextElement();
                final TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }

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

    public static void expandAll(final JTree tree, final boolean expand) {
        final TreeNode root = (TreeNode) tree.getModel().getRoot();

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

Related

  1. autoExpand(JTree tree, TreePath path, int maxLines, int maxChildToExpand, boolean dontExpandToLeafs)
  2. buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)
  3. expand(JTree tree, int depth)
  4. expand(JTree tree, int level)
  5. expand(JTree tree, int startingIndex, int rowCount)
  6. expandAll(JTree jTree)
  7. expandAll(JTree rightTree)
  8. expandAll(JTree t)
  9. expandAll(JTree tree)