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;
  }
Tests whether a tree contains a given node.

@param tree The tree to search.
@param node The node to search for.
@return True, if the node is part of the tree. False, otherwise.
java/zylib/src/main/java/com/google/security/zynamics/zylib/gui/jtree/TreeHelpers.java