void (JTree tree) { TreePath anchor = getAnchor(tree); if (anchor == null) return; // not found for (TreePath parent = anchor; parent != null; parent = parent.getParentPath()) { if (acceptDescendants(tree, parent, path -> !tree.isPathSelected(path), tree::addSelectionPaths)) { setAnchor(tree, anchor); return; // interrupt if some children were selected } if (!tree.isPathSelected(parent)) { if (!tree.isRootVisible() && parent.getParentPath() == null) return; // cannot select hidden root tree.addSelectionPath(parent); setAnchor(tree, anchor); return; // interrupt if parent was selected } } }