TreeExpansionMonitor<PackageDependenciesNode> (final JTree tree) { return new TreeExpansionMonitor<>(tree) { @Override protected TreePath findPathByNode(final PackageDependenciesNode node) { if (node.getPsiElement() == null) { return new TreePath(node.getPath()); } Enumeration<TreeNode> enumeration = ((DefaultMutableTreeNode)tree.getModel().getRoot()).breadthFirstEnumeration(); while (enumeration.hasMoreElements()) { final TreeNode nextElement = enumeration.nextElement(); if (nextElement instanceof PackageDependenciesNode child) { //do not include root if (child.equals(node)) { return new TreePath(child.getPath()); } } } return null; } }; }