Here you can find the source of getComponentsOfType(JComponent base, Class type)
Parameter | Description |
---|---|
base | non-null base tostart |
type | non-null type - must be a subtyoe of JCOmponent |
public static JComponent[] getComponentsOfType(JComponent base, Class type)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.awt.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; public class Main { /**/*from w w w . ja va 2 s. c o m*/ * gather all components which are instances of a specific class * * @param base non-null base tostart * @param type non-null type - must be a subtyoe of JCOmponent * @return non-null array of implementing components */ public static JComponent[] getComponentsOfType(JComponent base, Class type) { List<JComponent> holder = new ArrayList<JComponent>(); accumulateComponentsOfType(base, type, holder); JComponent[] ret = (JComponent[]) Array.newInstance(type, holder.size()); holder.toArray(ret); return ret; } protected static void accumulateComponentsOfType(JComponent base, Class type, List<JComponent> holder) { Component[] components = base.getComponents(); for (int i = 0; i < components.length; i++) { Component component = components[i]; if (type.isInstance(component)) holder.add((JComponent) component); } } }