Java tutorial
//package com.java2s; import javax.swing.*; import javax.swing.tree.*; import java.util.*; public class Main { public static void expandTree(JTree tree) { expandTree(tree, new TreePath(tree.getModel().getRoot())); } /** * Collapses all the child nodes of a path * * @param tree * the tree that contains the paths to collapse * @param path * the path to collapse */ public static void expandTree(JTree tree, TreePath path) { expandTree(tree, path, new Hashtable()); } protected static void expandTree(JTree tree, TreePath path, Hashtable lookedAt) { Object node = path.getLastPathComponent(); if (lookedAt.containsKey(node)) return; lookedAt.put(node, node); Vector paths = new Vector(); tree.makeVisible(path); int childCount = tree.getModel().getChildCount(node); for (int i = 0; i < childCount; i++) { Object child = tree.getModel().getChild(node, i); TreePath p = path.pathByAddingChild(child); expandTree(tree, p, lookedAt); } } }