void () { myLeftTree.addTreeExpansionListener(new TreeExpansionListener() { @Override public void treeCollapsed(TreeExpansionEvent event) { } @Override public void treeExpanded(TreeExpansionEvent event) { DefaultMutableTreeNode expandedNode = (DefaultMutableTreeNode)event.getPath().getLastPathComponent(); for (int i = 0; i < expandedNode.getChildCount(); i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode)expandedNode.getChildAt(i); if (child.getChildCount() == 0 && !isLooped(event.getPath(), child)) { Module module = ((MyUserObject)child.getUserObject()).myModule; for (Module dependency : getModuleDependencies(module)) { child.add(new DefaultMutableTreeNode(new MyUserObject(isInCycle(dependency), dependency))); } TreeUtil.sortRecursively(child, NODE_COMPARATOR); } } } private static boolean isLooped(TreePath path, DefaultMutableTreeNode child) { for (Object o : path.getPath()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)o; if (node != child && Comparing.equal(node.getUserObject(), child.getUserObject())) { return true; } } return false; } }); myLeftTree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { TreePath selectionPath = myLeftTree.getSelectionPath(); if (selectionPath != null) { myPathField.setText(StringUtil.join(selectionPath.getPath(), o -> { Object userObject = ((DefaultMutableTreeNode)o).getUserObject(); return userObject instanceof MyUserObject ? ((MyUserObject)userObject).myModule.getName() : ""; }, " : ")); DefaultMutableTreeNode selection = (DefaultMutableTreeNode)selectionPath.getLastPathComponent(); if (selection != null) { updateRightTree(((MyUserObject)selection.getUserObject()).myModule); } } } }); }