int (JTree tree, TreePath path) { if (myLevelsLeft == 0) return myExpansionLimit; TreeModel model = tree.getModel(); Object node = path.getLastPathComponent(); int levelDecrement = 0; if (!tree.isExpanded(path) && !model.isLeaf(node)) { tree.expandPath(path); levelDecrement = 1; myExpansionLimit--; } for (int i = 0; i < model.getChildCount(node); i++) { Object child = model.getChild(node, i); if (model.isLeaf(child)) continue; ExpandContext childContext = new ExpandContext(myExpansionLimit, myLevelsLeft - levelDecrement); myExpansionLimit = childContext.expand(tree, path.pathByAddingChild(child)); if (myExpansionLimit <= 0) return 0; } return myExpansionLimit; }