Here you can find the source of collapseSubTree(JTree tree, DefaultMutableTreeNode startNode, DefaultTreeModel model)
startNode
node
Parameter | Description |
---|---|
tree | the tree to collapse |
startNode | the starting point |
model | the tree model |
private static void collapseSubTree(JTree tree, DefaultMutableTreeNode startNode, DefaultTreeModel model)
//package com.java2s; /*/* w w w . j av a 2 s. com*/ The GUFF - The GNU Ultimate Framework Facility Copyright (C) Simeosoft di Carlo Simeone This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.util.Enumeration; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; public class Main { /** * Collapse the subtree starting at <code>startNode</code> node * @param tree the tree to collapse * @param startNode the starting point * @param model the tree model */ private static void collapseSubTree(JTree tree, DefaultMutableTreeNode startNode, DefaultTreeModel model) { if (startNode.isLeaf()) { return; } for (Enumeration e = startNode.children(); e.hasMoreElements();) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); collapseSubTree(tree, node, model); } TreePath tp = new TreePath(model.getPathToRoot(startNode)); if (tree.isExpanded(tp)) { tree.collapsePath(tp); // senza questa istruzione, in alcuni casi non riusciamo a riportare // lo scrolling ad un nodo espanso tree.scrollPathToVisible(tp); } } }