I have a CheckBoxGroup of some CheckBox and another CheckBox for select/deselect all CheckBox of CheckBoxGroup.
How can I implements this?
My code have some bug:
Code:
public class CheckBoxGroupExample extends LayoutContainer {
private CheckBox a;
private CheckBox b;
private CheckBox c;
private CheckBox all;
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
CheckBoxGroup groupCheck = new CheckBoxGroup();
groupCheck.setOrientation(Orientation.VERTICAL);
a = new CheckBox();
a.setBoxLabel("A");
a.addListener(Events.Change, new Listener<FieldEvent>() {
@Override
public void handleEvent(FieldEvent fe) {
manageAll();
}
});
groupCheck.add(a);
b = new CheckBox();
b.setBoxLabel("B");
b.addListener(Events.Change, new Listener<FieldEvent>() {
@Override
public void handleEvent(FieldEvent fe) {
manageAll();
}
});
groupCheck.add(b);
c = new CheckBox();
c.setBoxLabel("C");
c.addListener(Events.Change, new Listener<FieldEvent>() {
@Override
public void handleEvent(FieldEvent fe) {
manageAll();
}
});
groupCheck.add(c);
super.add(groupCheck);
all = new CheckBox();
all.setBoxLabel("Select/Deselect all");
all.addListener(Events.Change, new Listener<FieldEvent>() {
@Override
public void handleEvent(FieldEvent fe) {
Boolean checkedRaw = Boolean.valueOf(((CheckBox) fe.getField()).getRawValue());
boolean allSelected = false;
if (checkedRaw) {
allSelected = true;
}
a.setValue(allSelected);
b.setValue(allSelected);
c.setValue(allSelected);
}
});
super.add(all);
}
private void manageAll() {
if (a.getValue() && b.getValue() && c.getValue()) {
all.setRawValue("true");
} else {
all.setRawValue("false");
}
}
}
Thanks