Java tutorial
//package com.java2s; //License from project: Open Source License import java.awt.Component; import java.awt.Container; import java.util.ArrayList; import java.util.List; import javax.swing.JComponent; public class Main { private static JComponent getComponentWithName(Container owner, String name, Class<?> clazz) { for (Component c : owner.getComponents()) { if (clazz.isInstance(c) && ((JComponent) c).getName() != null && ((JComponent) c).getName().equals(name)) return (JComponent) c; else if (c instanceof JComponent) { JComponent b = getComponentWithName((JComponent) c, name, clazz); if (b != null) return b; } } return null; } public static List<JComponent> getComponents(Container owner, Class<?> clazz) { return getComponents(owner, clazz, false); } public static List<JComponent> getComponents(Container owner, Class<?> clazz, boolean onlyVisible) { List<JComponent> list = new ArrayList<JComponent>(); for (Component c : owner.getComponents()) { if (clazz.isInstance(c) && (!onlyVisible || c.isShowing())) list.add((JComponent) c); else if (c instanceof JComponent) { for (JComponent b : getComponents((JComponent) c, clazz, onlyVisible)) list.add(b); } } return list; } }