Here you can find the source of expandTree(JTree tree, boolean expand)
public static void expandTree(JTree tree, boolean expand)
//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); } } }