void (@NotNull final SimpleTree tree, final int direction) { final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths == null) return; ContainerUtil.sort(selectionPaths, new Comparator<>() { @Override public int compare(TreePath o1, TreePath o2) { return -direction * compare(tree.getRowForPath(o1), tree.getRowForPath(o2)); } private int compare(int x, int y) { return Integer.compare(x, y); } }); for (TreePath selectionPath : selectionPaths) { final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)selectionPath.getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeNode.getParent(); final int idx = parent.getIndex(treeNode); ((DefaultTreeModel)tree.getModel()).removeNodeFromParent(treeNode); ((DefaultTreeModel)tree.getModel()).insertNodeInto(treeNode, parent, idx + direction); } tree.addSelectionPaths(selectionPaths); }