Gabriel Pantiru Gabriel Pantiru - 1 year ago 106
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.!

Answer Source

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)){
   // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download