Here you can find the source of getExpansionState(JTree tree, int row)
public static String getExpansionState(JTree tree, int row)
//package com.java2s; //License from project: MIT License import javax.swing.JTree; import javax.swing.tree.TreePath; public class Main { public static String getExpansionState(JTree tree, int row) { TreePath rowPath = tree.getPathForRow(row); StringBuffer buf = new StringBuffer(); int rowCount = tree.getRowCount(); for (int i = row; i < rowCount; i++) { TreePath path = tree.getPathForRow(i); if (i == row || isDescendant(path, rowPath)) { if (tree.isExpanded(path)) buf.append("," + String.valueOf(i - row)); } else break; }/*w w w.ja v a 2 s . c om*/ return buf.toString(); } public static boolean isDescendant(TreePath path1, TreePath path2) { int count1 = path1.getPathCount(); int count2 = path2.getPathCount(); if (count1 <= count2) return false; while (count1 != count2) { path1 = path1.getParentPath(); count1--; } return path1.equals(path2); } }