DarioBB
DarioBB

Reputation: 663

Vaadin can't get combo box field value at 0 position

I'm trying to set field at index 0 in Vaadin combo box to default value, so I could avoid error message if user doesen't select anything. So I would like that instead of blank field I have populated field at index 0.

I have tried to set it and managed it with this:

field.setNullSelectionAllowed(true);
field.setNullSelectionItemId(container.getIdByIndex(0));

So I don't have blank value at index 0, instead my previous value of index 1 is now at index 0. And that is exactly what I want and need and in combo box looks just as I want.

But, unfortunately, when I submit my form, value is not passed. Only values after index 0 are passed. It's so frustrating, can somebody help me? Value passed to setNullSelectionItemId exists 100%.

How can I grab value from index at place 0 in combo box?

p.s. here is my code:

public Field<?> buildAndBindComboBox(final String caption, final BeanItemContainer<?> container,
        final Object propertyId, final String title, final ValueChangeListener listener, final boolean nullAllowed,
        final boolean required, final boolean enabled, final boolean visible) {

    @SuppressWarnings("serial")
    ComboBox field = new ComboBox(caption, container) {
        // http://dev.vaadin.com/ticket/10544
        // - typing in ComboBox causes Internal Error
        private boolean inFilterMode;

        @Override
        public void containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent event) {
            if (inFilterMode) {
                super.containerItemSetChange(event);
            }
        }

        @Override
        protected List<?> getOptionsWithFilter(boolean needNullSelectOption) {
            try {
                inFilterMode = true;
                return super.getOptionsWithFilter(needNullSelectOption);
            } finally {
                inFilterMode = false;
            }
        }
    };

    field.setStyleName("comboBox");
    field.setInputPrompt("Select");
    if(defaultValue == true){
        field.setNullSelectionAllowed(false);
        field.setNullSelectionItemId(container.getIdByIndex(0).toString());
        //field.select(container.getIdByIndex(0));
        //field.setValue(container.getIdByIndex(0));
        //field.setRequired(false);
        defaultValue = false;
    } else {
        field.setNullSelectionAllowed(nullAllowed);
        field.setRequired(required);
    }
    field.setImmediate(true);
    field.setNewItemsAllowed(false);
    field.setFilteringMode(FilteringMode.CONTAINS);
    if (title != null) {
        field.setItemCaptionPropertyId(title);
    }
    //field.setNullSelectionAllowed(nullAllowed);
    //field.setRequired(required);
    field.setVisible(visible);

    if (listener != null) {
        field.addValueChangeListener(listener);
    }

    this.bind(field, propertyId);

    field.setEnabled(enabled);

    return field;
}

public void setDefaultValueFirstItem(boolean def){
   defaultValue = def;
}

It is binded like this:

commitmentFeeBinder.setDefaultValueFirstItem(true);
commitmentFeeBinder.buildAndBindComboBox("No working day labels", noWorkingDays, "noWorkingDaysCF", "title", null, false, !transaCF, true, !transaCF);

Upvotes: 0

Views: 1638

Answers (2)

frss-soft.com
frss-soft.com

Reputation: 106

private void resetComboBoxToIndex(ComboBox combo, int index) {
    BeanItemContainer<Bean_ComboBox> items_combo = (BeanItemContainer<Bean_ComboBox>)combo.getContainerDataSource();
    if(items_combo != null && items_combo.size() > index) {
         Bean_ComboBox primerItem = items_combo.getIdByIndex(index);
         if(primerItem != null) {
                combo.select(primerItem);
        }
    }
}

Upvotes: 0

ripla
ripla

Reputation: 422

If I understood your question correctly, Steffen Harbich is correct in suggesting that if you want the first item to be selected by default you should disable null selection and select the first item by default. E.g. this works:

ComboBox cb = new ComboBox("", Arrays.asList("First", "Second", "Third"));
cb.setNullSelectionAllowed(false);
cb.select("First");

Or alternatively with a BeanItemContainer:

List<MyBean> beans = Arrays.asList(new MyBean("First"), new MyBean("Second"), new MyBean("Third"));
BeanItemContainer<MyBean> bic = new BeanItemContainer<>(MyBean.class, beans);
ComboBox cb = new ComboBox("", bic);
cb.setNullSelectionAllowed(false);
cb.select(bic.getIdByIndex(0));

Upvotes: 1

Related Questions