collapse JTree Path - Java Swing

Java examples for Swing:JTree

Description

collapse JTree Path

Demo Code


//package com.java2s;
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
    public static void collapsePath(JTree tree, TreePath parent) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                collapsePath(tree, path);
            }/* w ww  .  j  a  v  a  2s  .  co m*/
        }
        tree.collapsePath(parent);
    }
}

Related Tutorials