Gabriel Pantiru Gabriel Pantiru - 4 months ago 9x
Java Question

How to test if a Object is a Java AWT component?

I have a method wich will receive a String array containing a list of classes. I have to check wich one is a Java AWT component and return a list with the instances of those components. But I am stuck at testing whether or not a Object is a java Awt component or not. Can you please help me ! Thanks

EDIT: Thanks! It works with .isAsignableFrom() methot. I knew about the instanceof operator but didn't tought it would work.!


If you want to check the class hierarchy without creating instances first:

Class<?> clazz = Class.forName(className);  // errors out for bad name
if (Component.class.isAssignableFrom(clazz)){
   // ...