expand JTree Subtree - Java Swing

Java examples for Swing:JTree

Description

expand JTree Subtree

Demo Code


//package com.java2s;

import javax.swing.JTree;

import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Main {
    public static void expandSubtree(JTree tree, TreePath path) {
        TreeModel treeModel = tree.getModel();
        Object node = path.getLastPathComponent();
        for (int i = 0; i < treeModel.getChildCount(node); i++) {
            Object child = treeModel.getChild(node, i);
            TreePath childPath = path.pathByAddingChild(child);
            if (!isLeaf(tree, childPath)) {
                expandSubtree(tree, childPath);
            }/*from w  ww  .  j  a va  2 s.c om*/
        }
        tree.expandPath(path);
    }

    public static boolean isLeaf(JTree tree, TreePath path) {
        return tree.getModel().isLeaf(path.getLastPathComponent());
    }
}

Related Tutorials