String () { TreePath[] selection = comp.getSelectionPaths(); int initialDepth; JBIterable<TreePath> paths; JBTreeTraverser<TreePath> traverser = TreeUtil.treePathTraverser(comp).expand(comp::isExpanded); if (selection != null && selection.length > 1) { paths = traverser.withRoots(selection).unique().traverse(); initialDepth = Integer.MAX_VALUE; for (TreePath path : selection) { initialDepth = Math.min(initialDepth, path.getPathCount() - 1); } } else { initialDepth = comp.isRootVisible() ? 0 : 1; paths = traverser.traverse().skip(initialDepth); } TreeCellRenderer renderer = comp.getCellRenderer(); StringBuilder sb = new StringBuilder(); for (TreePath path : paths) { Object value = path.getLastPathComponent(); int depth = path.getPathCount() - initialDepth - 1; //noinspection StringRepeatCanBeUsed for (int i = 0; i < depth; i++) sb.append(" "); append(sb, value, renderer.getTreeCellRendererComponent(comp, value, false, false, false, -1, false)); sb.append("\n"); } return sb.toString(); }