MyTreeNode (@NotNull DefaultMutableTreeNode root, @NotNull List<String> path, int index) { String groupName = path.get(index ++); for (int i = 0; i < root.getChildCount(); i ++) { TreeNode child = root.getChildAt(i); if (child instanceof MyTreeNode && groupName.equals(child.toString())) { return index < path.size() - 1 ? ensureGroup((MyTreeNode)child, path, index) : (MyTreeNode)child; } } MyTreeNode groupNode = new MyTreeNode(groupName); root.add(groupNode); return index < path.size() - 1 ? ensureGroup(groupNode, path, index) : groupNode; }