Java examples for java.awt:Container
Returns all elements of a specific class from a container.
//package com.java2s; import java.awt.Component; import java.awt.Container; import java.util.ArrayList; import java.util.List; public class Main { /**/*from w w w .j av a 2 s .c o m*/ * Returns all elements of a specific class from a container. * * <em>Only elements of that class are detected, not sub- and supertyes!</em> * * @param <T> class type * @param container container * @param clazz class * @return found elements or empty list */ public static <T> List<T> getAllOf(Container container, Class<T> clazz) { if (container == null) { throw new NullPointerException("container == null"); } if (clazz == null) { throw new NullPointerException("clazz == null"); } List<T> components = new ArrayList<>(); addAllOf(container, clazz, components); return components; } @SuppressWarnings("unchecked") private static <T> void addAllOf(Container container, Class<T> clazz, List<T> all) { int count = container.getComponentCount(); if (container.getClass().equals(clazz)) { all.add((T) container); } for (int i = 0; i < count; i++) { Component component = container.getComponent(i); if (component instanceof Container) { addAllOf((Container) component, clazz, all); // Recursive } else if (component.getClass().equals(clazz)) { all.add((T) component); } } } }