Here you can find the source of pathToDepth(TreePath path, int depth)
public static TreePath pathToDepth(TreePath path, int depth)
//package com.java2s; //License from project: Open Source License import javax.swing.tree.TreePath; public class Main { public static TreePath pathToDepth(TreePath path, int depth) { TreePath returnPath = path; if (path.getPathCount() < depth) return new TreePath(new Object[0]); else {/*from w w w. j a v a2 s . c o m*/ for (int i = path.getPathCount(); i > (depth + 1); i--) { returnPath = returnPath.getParentPath(); } } return returnPath; } }