Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Component> getAllComponents(final Container c) {
        Component[] comps = c.getComponents();
        List<Component> compList = new ArrayList<Component>();
        for (Component comp : comps) {
            compList.add(comp);
            if (comp instanceof Container)
                compList.addAll(getAllComponents((Container) comp));
        }
        return compList;
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> getAllComponents(final Container c, Class<T> classType) {
        Component[] comps = c.getComponents();
        List<T> compList = new ArrayList<T>();
        for (Component comp : comps) {
            if (classType.isInstance(comp)) {
                compList.add((T) comp);
            }
            if (comp instanceof Container) {
                compList.addAll(getAllComponents((Container) comp, classType));
            }
        }
        return compList;
    }
}