Here you can find the source of expand(JTree tree, int level)
public static void expand(JTree tree, int level)
//package com.java2s; //License from project: Apache License import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; public class Main { public static void expand(JTree tree, int level) { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) tree.getModel().getRoot(); while (currentNode != null) { if (currentNode.getLevel() <= level) { tree.expandPath(new TreePath(currentNode.getPath())); }// www .ja va2 s . c om currentNode = currentNode.getNextNode(); } } }