lundi 30 novembre 2015

Reflection on swing components

I'm trying to assign a value to a swing component through reflection. Let's use a JCheckBox for example. I have the following class:

public class JCheckBoxTest
{
    private JCheckBox test;

    public JCheckBoxTest()
    {
        this.test = new JCheckBox();
    }

    public reflectionTest()
    {
        Field field;
        Method method;

        field = this.getClass().getDeclaredField("test");
        method = field.getType().getSuperclass().getDeclaredMethod("setSelected");

        method.invoke(field, "true");
    }
}

This code fails at:

method = field.getType().getSuperclass().getDeclaredMethod("setSelected");

because it cannot find the specified "setSelected" method since it is located inside the inner class "ToggleButtonModel" of the superclass "JToggleButton" which is extended by the "JCheckBox" class.

What would be the best approach to solve this?

Thanks.

Edit: Corrected typo in code.





Aucun commentaire:

Enregistrer un commentaire