Java JTree Expand setExpandedPaths(JTree tree, TreePath[] expandedPaths)

Here you can find the source of setExpandedPaths(JTree tree, TreePath[] expandedPaths)

Description

Expand all the previously remembered expanded paths.

License

Apache License

Declaration

public static void setExpandedPaths(JTree tree, TreePath[] expandedPaths) 

Method Source Code

//package com.java2s;
/*//  www. j  a  va  2s. c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Main {
    /**
     * Expand all the previously remembered expanded paths.
     */
    public static void setExpandedPaths(JTree tree, TreePath[] expandedPaths) {
        if (expandedPaths == null) {
            return;
        }
        for (int i = 0; i < expandedPaths.length; ++i) {
            TreePath oldPath = expandedPaths[i];
            TreePath newPath = searchPath(tree.getModel(), oldPath);
            if (newPath != null) {
                tree.expandPath(newPath);
            }
        }
    }

    /**
     * Search for a path in the specified tree model, whose nodes have
     * the same name (compared using <code>equals()</code>)
     * as the ones specified in the old path.
     *
     * @return a new path for the specified model, or null if no such path
     *         could be found.
     */
    public static TreePath searchPath(TreeModel model, TreePath oldPath) {
        Object treenode = model.getRoot();
        Object[] oldPathNodes = oldPath.getPath();
        TreePath newPath = new TreePath(treenode);
        for (int i = 0; i < oldPathNodes.length; ++i) {
            Object oldPathNode = oldPathNodes[i];
            if (treenode.toString().equals(oldPathNode.toString())) {
                if (i == (oldPathNodes.length - 1)) {
                    return newPath;
                } else {
                    if (model.isLeaf(treenode)) {
                        return null; // not found
                    } else {
                        int count = model.getChildCount(treenode);
                        boolean foundChild = false;
                        for (int j = 0; j < count; ++j) {
                            Object child = model.getChild(treenode, j);
                            if (child.toString().equals(oldPathNodes[i + 1].toString())) {
                                newPath = newPath.pathByAddingChild(child);
                                treenode = child;
                                foundChild = true;
                                break;
                            }
                        }
                        if (!foundChild) {
                            return null; // couldn't find child with same name
                        }
                    }
                }
            }
        }
        return null;
    }
}

Related

  1. isTreePathExpandable(TreeModel treeModel, TreePath treePath)
  2. makeTreeAutoExpandable(JTree tree)
  3. searchUnexpandedPath(JTree tree, TreePath path, int index, TreeNode node, TreePath[] result, boolean compareOnlyLabels)
  4. setExpandedIcon(JTree tree, Icon icon)
  5. setExpandedOnEdt(JTree tree, TreePath path, boolean expanded)
  6. setFullyExpandedPath(JTree Target, TreeModel TheModel, Object Node, Vector Path)