Java JTree Expand getLastExpandedNodes(final JTree tree)

Here you can find the source of getLastExpandedNodes(final JTree tree)

Description

get Last Expanded Nodes

License

Open Source License

Declaration

public static List<DefaultMutableTreeNode> getLastExpandedNodes(final JTree tree) 

Method Source Code


//package com.java2s;
/*/*from  w  w w.j a  v a  2 s.c o m*/
Copyright 2015 Google Inc. All Rights Reserved.
    
Licensed 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 java.util.ArrayList;

import java.util.List;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreePath;

public class Main {
    public static List<DefaultMutableTreeNode> getLastExpandedNodes(final JTree tree) {
        final List<DefaultMutableTreeNode> nodeList = new ArrayList<DefaultMutableTreeNode>();

        final int rowCount = tree.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            final TreePath path = tree.getPathForRow(i);
            DefaultMutableTreeNode lastPathComponent = null;

            try {
                lastPathComponent = (DefaultMutableTreeNode) path.getLastPathComponent();
            } catch (final Exception e) {
                throw new IllegalArgumentException(
                        "Cast failed! JTree must contain DefaultMuteableTreeNode or derived instances.");
            }

            if (lastPathComponent.isLeaf() || !tree.isExpanded(path)) {
                nodeList.add((DefaultMutableTreeNode) lastPathComponent.getParent());
            }
        }
        return nodeList;
    }
}

Related

  1. expandTreeLevels(JTree tree, TreePath parent, boolean expand, int desiredLevel)
  2. expandTreePaths(JTree tree, Enumeration expandPaths)
  3. fullExpand(JTree tree, TreePath parentPath, int nExpansions)
  4. getExpandedPaths(JTree tree)
  5. getExpandedPaths(JTree tree)
  6. getPaths(JTree tree, boolean expanded)
  7. getTreeSelectedExpandedIcon()
  8. isTreePathExpandable(TreeModel treeModel, TreePath treePath)
  9. makeTreeAutoExpandable(JTree tree)