
org.bidib.wizard.mvc.common.view.ComponentUtils Maven / Gradle / Ivy
package org.bidib.wizard.mvc.common.view;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
public class ComponentUtils {
public static List harvestComponents(Container c, Class clazz) {
List jLabels = new ArrayList();
harvestComponents(c, clazz, jLabels);
return jLabels;
}
public static void harvestComponents(Container c, Class clazz, List l) {
Component[] components = c.getComponents();
for (Component com : components) {
if (clazz.isAssignableFrom(com.getClass())) {
l.add((T) com);
}
else if (com instanceof Container) {
harvestComponents((Container) com, clazz, l);
}
}
}
/**
* Find the first component of the specified class in the container.
*
* @param c
* the container
* @param clazz
* the class
* @return the first found component or null if none was found
*/
public static T harvestComponent(Container c, Class clazz) {
Component[] components = c.getComponents();
for (Component com : components) {
if (clazz.isAssignableFrom(com.getClass())) {
return ((T) com);
}
else if (com instanceof Container) {
T result = harvestComponent((Container) com, clazz);
if (result != null) {
return result;
}
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy