Java tutorial
//package com.java2s; import javax.swing.JTree; public class Main { public static void fullSelectionCollapse(JTree tree) { int startRow = tree.getLeadSelectionRow(); int stopRow = getStopRow(tree, startRow); for (int i = stopRow - 1; i >= startRow; --i) { tree.collapseRow(i); } } private static int getStopRow(JTree tree, int startRow) { int startDepth = tree.getPathForRow(startRow).getPathCount(); int last = tree.getRowCount(); int stopRow = last; for (int i = startRow + 1; i < last; ++i) { int depth = tree.getPathForRow(i).getPathCount(); if (depth <= startDepth) { stopRow = i; break; } } return stopRow; } }