get All Sub Components - Java Swing

Java examples for Swing:JComponent

Description

get All Sub Components

Demo Code


//package com.java2s;

import java.awt.Component;
import java.awt.Container;

import java.util.Arrays;

import java.util.LinkedList;
import java.util.List;

import javax.swing.JComponent;

public class Main {
    public static JComponent[] getAllSubComponents(Container root) {
        List<JComponent> comps = new LinkedList<JComponent>();
        for (Component c : root.getComponents()) {
            try {
                comps.add((JComponent) c);
                comps.addAll(Arrays
                        .asList(getAllSubComponents((JComponent) c)));
            } catch (final ClassCastException e) {
                continue;
            }/*w ww. j a v a  2s .  c  om*/
        }
        return comps.toArray(new JComponent[comps.size()]);
    }
}

Related Tutorials