Here you can find the source of expandOrCollapseJTree(JTree tree, boolean expand)
Parameter | Description |
---|---|
tree | a parameter |
expand | true means expand, false collapse |
public static void expandOrCollapseJTree(JTree tree, boolean expand)
//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); } } }