Bind checkbox

Hi,

I have a column on my Oracle table, for repeating/recurring meetings:-

repeats NUMBER(1);

How can i bind this using JPA, to my fieldgroup using a checkbox?

John.

Any ideas here? Same question to bind it to a checkbox in a table (note, not using a bean with a boolean, as Oracle doesn’t support this)?

Can you expose the property as a boolean? Even if database can’t handle it, you just make a conversion from number to boolean before dealing with the CheckBox. For instance, just add another getter and setter in your entity exposing the Integer as a Boolean. The getther will return true if the Integer value != 0 or false if == 0. The setther will set 1 if argument is true and 0 if argument is false. Will this work for you?

Actually this might be what you need:

import com.vaadin.data.util.converter.Converter;
        
Converter<Boolean, Integer> converter = new Converter<Boolean, Integer>() {

    @Override
    public Integer convertToModel(Boolean value,
            Class<? extends Integer> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {

        return value ? 1 : 0;
    }

    @Override
    public Boolean convertToPresentation(Integer value,
            Class<? extends Boolean> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {

        return value != 0;
    }

    @Override
    public Class<Integer> getModelType() {
        return Integer.class;
    }

    @Override
    public Class<Boolean> getPresentationType() {
        return Boolean.class;
    }
};

checkBox.setConverter(converter);

Thanks for the reply. I get a compiler error in Eclipe:-

Locale cannot be resolved to a type…

Any ideas?

Hi, sure. Just add this import:

import java.util.Locale;

Plese let me know if all is fine.

All the best!

When you have compile errors in Eclipse, a simple way to fix them is to move the cursor on the text with the error and to press
CTRL + 1
in Windows or
CMD + 1
on Mac. You’ll get a list of suggestions on how to fix that error. Usually just picking the right suggestion is enough.

In case on an import error you need to be sure of the package where that class belongs, otherwise, importing the class from a different package then the expected one will bring yet another error with a class type conflict, the expected type of the value doesn’t match the one provided, orso. The easiest way to figure out the right class type including the right package is to Open Declaration (
F3
). In case of Vaadin, where source code is available, it will lead you to the right place in the our code where you can see exactly what types are used, eventually looking also at the imports there, or just take the F3 further to jump in the declarations of the type used.

It works like a dream. I now have the checkbox with persistence (using number(1) Oracle datatype). Thanks a million!

Can i do the same/similar in a bound table?

Do you have the checkboxes visible in your table already?

For a Table component, have a look at
table.setTableFieldFactory
mehod and
TableFieldFactory
.

This might be enough for you but I can’t tell for certain until you test your code, you might need to adapt it, so use the idea only. As you can see you need to create one CheckBox for each row and set its value.

table.setTableFieldFactory(new TableFieldFactory() {

    @Override
    public Field<?> createField(Container container,
            final Object itemId, Object propertyId, Component uiContext) {

        Integer value = (Integer) container.getItem(itemId)
                .getItemProperty(propertyId).getValue();

        CheckBox checkBox = new CheckBox();
        checkBox.setValue(value != 0);

        return checkBox;
    }
});

The only issue left here is refreshing the table when you change the values. You might need to bind the comboBox created above with the property and item in the container, or just call a refresh on the table, which is not great but works more or less depending on the amount of data displayed.

I don’t. My entity class only returns an int #(marking if a user is present or not i.e. 1 or 0) and this is displayed in the table using JPAContainer. I don’t seem to be able to display and set this using a checkbox…

Then the sample code I wrote previously should show checkboxes with the correct value. But you also need to check on the propertyId, which actually represent the column of the table and return the checkBox only if it’s the number column you’re talking, otherwise just return null. I actually forgot about this matter early when I wrote you that.

Do you need to edit the values from the table? Or the table should be read only?

Hi, yes, it should be updateable by the user (they user to flag is another user is present).

Thanks in advance.

I wrote previously ComboBox instead of CheckBox, I’ve chenge it now, sorry about that.

If you have your data Container set to the Table, this is all you need to do in order to show an editable column with CheckBoxes which will set the values automatically in the data Container:

        table.setEditable(true);
        table.setTableFieldFactory(new TableFieldFactory() {

            @Override
            public Field<?> createField(Container container,
                    final Object itemId, Object propertyId, Component uiContext) {

                // "repeats" is the id of the property defining your column
                if (propertyId.equals("repeats")) {
                    CheckBox checkBox = new CheckBox();

                    // Here set the converter defined previously, which worked also for
                    // your other CheckBox.
                    checkBox.setConverter(converter);

                    return checkBox;

                } else {
                    return null;
                }
            }

        });

My friend, you have done it again, works just as we need it to. Thank you so much!