expand All JTree Children Node - Java Swing

Java examples for Swing:JTree

Description

expand All JTree Children Node

Demo Code


//package com.java2s;
import javax.swing.*;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import java.util.Enumeration;

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

        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
}

Related Tutorials