Set<Object> () { TreePath[] paths = myTree.getSelectionPaths(); Set<Object> result = new LinkedHashSet<>(); if (paths != null) { for (TreePath eachPath : paths) { if (eachPath.getLastPathComponent() instanceof DefaultMutableTreeNode eachNode) { if (eachNode == myRootNode && !myTree.isRootVisible()) continue; Object eachElement = getElementFor(eachNode); if (eachElement != null) { result.add(eachElement); } } } } return result; }