Checkbox with values 0 and 1

Is it possible to config checkbox to set/return values 0 (as false) and 1 (as true) ?
I want to use the checkbox with integer fields.

Hello,

Unfortunately, CheckBox does not have a generic type. It is intended to use only with Boolean type.

1 Like

There is a workaround for CheckBox with numbers. CUBA enables to add/override component implementations. For this, you should create:

  1. Interface that should inherit Field<Integer>
CustomCheckBox interface
public interface CustomCheckBox extends Field<Integer>, Buffered, Component.Focusable {

    String NAME = "customCheckBox";
}
  1. Implementation of this interface
WebCustomCheckBox
public class WebCustomCheckBox extends WebV8AbstractField<com.vaadin.ui.CheckBox, Boolean, Integer> implements CustomCheckBox {

    public WebCustomCheckBox() {
        component = createComponent();
        internalValue = 0;

        attachValueChangeListener(component);
    }

    protected CubaCheckBox createComponent() {
        return new CubaCheckBox();
    }

    @Override
    protected Integer convertToModel(Boolean componentRawValue) throws ConversionException {
        return BooleanUtils.toIntegerObject(componentRawValue);
    }

    @Override
    protected Boolean convertToPresentation(Integer modelValue) throws ConversionException {
        return BooleanUtils.toBooleanObject(modelValue);
    }

    @Override
    public void focus() {
        component.focus();
    }

    @Override
    public int getTabIndex() {
        return component.getTabIndex();
    }

    @Override
    public void setTabIndex(int tabIndex) {
        component.setTabIndex(tabIndex);
    }

    @Override
    public void setParent(Component parent) {
        super.setParent(parent);

        if (parent instanceof FieldGroup
                || parent instanceof Form) {
            ((CubaCheckBox) component).setCaptionManagedByLayout(true);
        }
    }

    @Override
    public void commit() {
        super.commit();
    }

    @Override
    public void discard() {
        super.discard();
    }

    @Override
    public boolean isBuffered() {
        return super.isBuffered();
    }

    @Override
    public void setBuffered(boolean buffered) {
        super.setBuffered(buffered);
    }

    @Override
    public boolean isModified() {
        return super.isModified();
    }
}
  1. Loader
CustomCheckBoxLoader
public class CustomCheckBoxLoader extends AbstractFieldLoader<CustomCheckBox> {

    @Override
    public void createComponent() {
        resultComponent = factory.create(CustomCheckBox.NAME);
        loadId(resultComponent, element);
    }

    @Override
    public void loadComponent() {
        super.loadComponent();

        loadTabIndex(resultComponent, element);
        loadBuffered(resultComponent, element);
    }
}

Then you should register this component. Create ui-components.xml in WEB module:

<components xmlns="http://schemas.haulmont.com/cuba/components.xsd">
    <component>
        <name>customCheckBox</name>
        <class>com.company.fcheckbox.web.component.WebCustomCheckBox</class>
        <componentLoader>com.company.fcheckbox.web.component.CustomCheckBoxLoader</componentLoader>
    </component>
</components>

In web-app.properties add cuba.web.componentsConfig property:

cuba.web.componentsConfig = +com/company/fcheckbox/ui-components.xml

After that, you will able to use customCheckBox element in the screen descriptor and create a CustomCheckBox component using UiComponents. For more details, see the demo project: fcheckbox.zip (85.1 KB)

1 Like