mercredi 10 juillet 2019

How to cast Field to specified type

I'm trying to make an user interface using Swing, but I don't want to have to manually include every component in my component array.

So far the best attempt I got throws an exception, which I can't really have.


for (Field f : this.getClass().getFields()) {
    if (f.getType().isAssignableFrom(JComponent.class)) {
        JComponent field = (JComponent) f.get(JComponent.class);
        components.add(field);
    }
}

I wanted the code I showcased to not include any exception, but it does. Is there any way to do this without having the risk of an exception?





Aucun commentaire:

Enregistrer un commentaire