Here you can find the source of getLastExpandedNodes(final JTree tree)
public static List<DefaultMutableTreeNode> getLastExpandedNodes(final JTree tree)
//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; } }