Expand all JTree nodes under the specified DefaultMutableTreeNode object. - Java Swing

Java examples for Swing:JTree

Description

Expand all JTree nodes under the specified DefaultMutableTreeNode object.

Demo Code

//package com.java2s;

import java.util.Enumeration;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import javax.swing.tree.TreePath;

public class Main {
    /**//from   ww w.  j a v  a  2s.com
     * Expand all tree nodes under the specified DefaultMutableTreeNode object.
     * @param treeNode
     * @param tree a JTree with DefaultTreeModel as its model.
     */
    public static void expandAllNodes(DefaultMutableTreeNode treeNode,
            JTree tree) {
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        Enumeration enum1 = treeNode.breadthFirstEnumeration();
        while (enum1.hasMoreElements()) {
            treeNode = (DefaultMutableTreeNode) enum1.nextElement();
            if (treeNode.getChildCount() > 0) {
                TreePath path = new TreePath(model.getPathToRoot(treeNode));
                tree.expandPath(path);
            }
        }
    }
}

Related Tutorials