question: private void collapseExpanded ( final TreePath newPath ) { if ( ! m_stayExpandedPaths . isEmpty ( ) ) { TreePath root = newPath ; while ( root . getPathCount ( ) > 1 ) { root = root . getParentPath ( ) ; } final Enumeration < TreePath > expandedDescendants = m_tree . getExpandedDescendants ( root ) ; final TreePath selectedPath = m_tree . getSelectionPath ( ) ; final Stack < TreePath > collapsePaths = new Stack < > ( ) ; while ( expandedDescendants . hasMoreElements ( ) ) { final TreePath currentDescendant = expandedDescendants . nextElement ( ) ; if ( ! currentDescendant . isDescendant ( newPath ) && ( selectedPath == null || ! currentDescendant . isDescendant ( selectedPath ) ) ) { collapsePaths . add ( currentDescendant ) ; } } if ( ! collapsePaths . isEmpty ( ) ) { for ( final TreePath currentPath : collapsePaths ) { m_tree . collapsePath ( currentPath ) ; } m_stayExpandedPaths . removeAll ( collapsePaths ) ; } } }
target:  collapses expanded paths except if new path is a descendant