Here you can find the source of expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList
private static void expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList<String> expansionStateStrings)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Enumeration; import javax.swing.JTree; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Main { public static void expandByExpansionStateStrings(JTree tree, ArrayList<String> expansionStateStrings) { TreeNode root = (TreeNode) tree.getModel().getRoot(); tree.cancelEditing();//from w w w .j av a 2 s. c o m expandByExpansionStateStrings(tree, new TreePath(root), expansionStateStrings); } private static void expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList<String> expansionStateStrings) { 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); expandByExpansionStateStrings(tree, path, expansionStateStrings); } } if (isTreePathContainedInExpansionState(parent, expansionStateStrings)) { tree.expandPath(parent); } else { tree.collapsePath(parent); } } private static boolean isTreePathContainedInExpansionState(TreePath treePath, ArrayList<String> expansionStateStrings) { if (treePath != null && expansionStateStrings != null) { return expansionStateStrings.contains(treePath.toString()); } return false; } }