samedi 2 octobre 2021

Java Wicket: Uncheck another checkbox (which is defined after the main checkbox) if the main checkbox is unchecked

I have two checkbox like this:

CheckBox checkBox1 = new AjaxCheckBox("enableCheckBox1", new PropertyModel<Boolean>(channelModel, "checkBox1")) {

    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        if (!getModelObject()) {
           // How can I uncheck checkBox2 when checkBox1 is unchecked
           checkBox2.setModelObject(false); // compilation error as checkBox2 is defined after this line.
           target.add(checkBox2); // same compilation error
        }
    }
};

CheckBox checkBox2 = new AjaxCheckBox("enableCheckbox2", new PropertyModel<Boolean>(channelModel, "checkBox2")) {

    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        if (getModelObject()) {
           checkBox1.setModelObject(true);
           target.add(checkBox1);
        }
    }
};

So basically, when I check checkBox2, I would like checkBox1 to be auto checked, which I am able to do. But if I want to uncheck checkBox2 if checkBox1 is unchecked, how do I do that? I am encountering compilation error as checkBox2 is define after the line using it. Is there another way to get the checkBox property other than calling the instance?




Aucun commentaire:

Enregistrer un commentaire