Java Wicket:如果选中复选框,则选中另一个复选框

Java Wicket: check another checkbox if checkbox checked

我有两个这样的复选框:

    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
            }
        };

所以基本上,当我选中 checkBox2 时,我希望 checkBox1 也被自动选中。我该怎么做?

您必须更改 checkBox1 的模型对象并更新它:

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

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