Java JTree Expand expandTreePaths(JTree tree, Enumeration expandPaths)

Here you can find the source of expandTreePaths(JTree tree, Enumeration expandPaths)

Description

Used to restore expansion state of the tree like:
 Enumeration expandPaths = tree.getExpandedDescendants(pathToRoot); // do some changes in the tree model ... 

License

Open Source License

Parameter

Parameter Description
tree a parameter
expandPaths a parameter

Declaration

public static void expandTreePaths(JTree tree, Enumeration<TreePath> expandPaths) 

Method Source Code

//package com.java2s;
/*/*from   ww  w . j a va  2 s.c  o m*/
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 *     Copyright (C) 2012, Maxim Zakharenkov
 *
 *   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.TreePath;

public class Main {
    /**
     * Used to restore expansion state of the tree like:
     * <pre>
     *      Enumeration<TreePath> expandPaths = tree.getExpandedDescendants(pathToRoot);
     *       
     *       // do some changes in the tree model
     *       ...
     *       
     *       // fire change event
     *       DefaultTreeModel model = (DefaultTreeModel)treProblems.getModel();
     *       model.nodeStructureChanged(root);
     *       
     *       // restore  expanded state
     *       GuiUtils.expandTreePaths(tree, expandPaths);
     * </pre>
     * It is useful only when DefaultMutableTreeNode elements or their successors 
     * are used inside tree.
     * @param tree
     * @param expandPaths
     */
    public static void expandTreePaths(JTree tree, Enumeration<TreePath> expandPaths) {
        if (expandPaths != null) {

            DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();

            // iterator through paths to expand
            while (expandPaths.hasMoreElements()) {
                // obtaion last path user object
                TreePath curPath = expandPaths.nextElement();
                DefaultMutableTreeNode castedNode = (DefaultMutableTreeNode) curPath.getLastPathComponent();
                Object obj = castedNode.getUserObject();

                // go through all elements in the tree and search for elements to expand
                // matching them by user object
                Enumeration<?> enumAll = root.breadthFirstEnumeration();
                while (enumAll.hasMoreElements()) {
                    DefaultMutableTreeNode curNode = (DefaultMutableTreeNode) enumAll.nextElement();
                    if (obj.equals(curNode.getUserObject())) {
                        // expand
                        tree.expandPath(new TreePath(curNode.getPath()));
                        break;
                    }
                }
            }
        }
    }
}

Related

  1. expandPathOnEdt(final JTree tree, final TreePath path)
  2. expandPaths(JTree tree, Collection paths)
  3. expandTree(JTree tree, boolean expand)
  4. expandTree(JTree tree, TreeNode start, int level)
  5. expandTreeLevels(JTree tree, TreePath parent, boolean expand, int desiredLevel)
  6. fullExpand(JTree tree, TreePath parentPath, int nExpansions)
  7. getExpandedPaths(JTree tree)
  8. getExpandedPaths(JTree tree)
  9. getLastExpandedNodes(final JTree tree)