get All Components from AWT Container - Java java.awt

Java examples for java.awt:Component

Description

get All Components from AWT Container

Demo Code


//package com.java2s;
import java.awt.*;

import java.util.*;

public class Main {
    public static java.util.List<Component> getAllComponents(
            final Container container) {
        Component[] comps = container.getComponents();
        java.util.List<Component> compList = new ArrayList<>();
        for (Component comp : comps) {
            compList.add(comp);/*from w ww  . jav  a 2 s  .c  o  m*/
            if (comp instanceof Container) {
                compList.addAll(getAllComponents((Container) comp));
            }
        }
        return compList;
    }

    public static java.util.List<Component> getComponents(
            Container container, String classString) {
        java.util.List<Component> list = new ArrayList<>();
        for (Component component : getAllComponents(container)) {
            if (component.getClass().toString().endsWith(classString)) {
                list.add(component);
            }
        }
        return list;
    }
}

Related Tutorials