Why I can't set a converter to a ComboBox?

I’ve made a converter:

import com.vaadin.data.util.converter.Converter;

public class BooleanToDateConverter implements Converter<Boolean, Date> {
    private static final long serialVersionUID = 1L;

    @Override
    public Date convertToModel(Boolean value, Class<? extends Date> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if ((Boolean) value == true) {
            return new Date();
        } else {
            return null;
        }
    }

    @Override
    public Boolean convertToPresentation(Date value, Class<? extends Boolean> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if (value == null) {
            return false;
        } else {
            return true;
        }
    }
    
    @Override
    public Class<Date> getModelType() {
        return Date.class;
    }

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

Then I have a Vaadin ComboBox myComboBox.

I try to set my converter to it: myComboBox.setConverter(new BooleanToDateConverter());
Then I get an error in Eclipse saying:

The method setConverter(Class<?>) in the type AbstractField<Object> is not applicable for the arguments (BooleanToDateConverter)

However, I’ve seen other converters being used similarly and they don’t get errors. Why?

Hi Vesa,
from pure coding point of view, you should convert from Object instead of Boolean, because ComboBox extends AbstractField. But what’s the purpose of converter in this case? Consider using item captions instead (chapter 5.5 3): https://vaadin.com/book/vaadin7/-/page/components.selection.html

With best regards,
Sergey.

Thanks. I even tried it with Object instead of Boolean but it still wouldn’t work.

Anyway, I’m ditching the converter plan and going back to the item caption mode research.

Currently I’m trying to make it like this: The ComboBox should have options “On Vacation” and “Present”.

The Combobox is part of a form the fields of which are bound to a datasource. So the form represents the properties of a Person bean. Now, in the database the Person table has a column “status”. When “On Vacation” is selected in the combobox and Save-button is pressed, I’d like to have a new date to appear into the database for that person’s row in “status” column. If “Present” is selected and save-button is pressed, I’d like a NULL value to be saved to that same place.

That way a null value shows that the person is not on vacation, and a date value shows that he/she is on a vacation. How can I pass a null value in a databound component without getting a NullPointerException?

You can’t pass null as item, but you can do like this:

final Object nullItem = "null-item";
ComboBox comboBox = new ComboBox();
comboBox.addItem(new Date());
comboBox.addItem(nullItem);
comboBox.setNullSelectionItemId(nullItem);
comboBox.setItemCaption(nullItem, "Null value caption");

Best regards,
Sergey.

Thank you, Sergey. Most valuable help.

You are welcome!

Best regards,
Sergey.