get Default Mutable Tree Node Children for JTree - Java Swing

Java examples for Swing:JTree

Description

get Default Mutable Tree Node Children for JTree

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<DefaultMutableTreeNode> getDefaultMutableTreeNodeChildren(
            TreeNode node) {/*from w  w  w .ja  v a  2  s  .  c  o m*/
        if (node == null) {
            throw new NullPointerException("node == null");
        }
        List<DefaultMutableTreeNode> children = new ArrayList<>(
                node.getChildCount());
        for (Enumeration<?> enumeration = node.children(); enumeration
                .hasMoreElements();) {
            Object nextElement = enumeration.nextElement();

            if (nextElement instanceof DefaultMutableTreeNode) {
                children.add((DefaultMutableTreeNode) nextElement);
            }
        }
        return children;
    }
}

Related Tutorials