Home > front end >  Java Wicket: check another checkbox if checkbox checked
Java Wicket: check another checkbox if checkbox checked

Time:10-03

I have two checkbox like this:

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

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

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                // check both checkBox1 and checkBox2
            }
        };

So basically, when I check checkBox2, I would like checkBox1 to be auto check as well. How do I do that?

CodePudding user response:

You have to change checkBox1's model object and update it:

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

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