Java examples for Swing:JTree
expand JTree Subtree
//package com.java2s; import javax.swing.JTree; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; public class Main { public static void expandSubtree(JTree tree, TreePath path) { TreeModel treeModel = tree.getModel(); Object node = path.getLastPathComponent(); for (int i = 0; i < treeModel.getChildCount(node); i++) { Object child = treeModel.getChild(node, i); TreePath childPath = path.pathByAddingChild(child); if (!isLeaf(tree, childPath)) { expandSubtree(tree, childPath); }/*from w ww . j a va 2 s.c om*/ } tree.expandPath(path); } public static boolean isLeaf(JTree tree, TreePath path) { return tree.getModel().isLeaf(path.getLastPathComponent()); } }