get JTree Node Children - Java Swing

Java examples for Swing:JTree

Description

get JTree Node Children

Demo Code


//package com.java2s;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeNode;

public class Main {
    public static List<TreeNode> getTreeNodeChildren(TreeNode node) {
        if (node == null) {
            throw new NullPointerException("node == null");
        }//  ww  w  .  j a v  a2 s  .com
        List<TreeNode> children = new ArrayList<>(node.getChildCount());
        for (Enumeration<?> enumeration = node.children(); enumeration
                .hasMoreElements();) {
            Object nextElement = enumeration.nextElement();
            if (nextElement instanceof TreeNode) {
                children.add((DefaultMutableTreeNode) nextElement);
            }
        }
        return children;
    }
}

Related Tutorials