0

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);
DarioBB
  • 663
  • 2
  • 8
  • 29
  • What other items do you have in your com box? In vaadin you usually don't work with indices, but directly with the objects – André Schild Apr 29 '16 at 09:24
  • You could disable null selection and initially select the item at index 0 via setValue of combo box. – Steffen Harbich Apr 29 '16 at 09:26
  • Steffen Harbich already tried that, doesen't work. I tried with select(), setValue(), various combinations - nothing works. Andre, I have other combo box and text box and none combo box except this should have default value chosen. Seems I must have blank value and choose it or it won't work.. I can't believe it is so difficult to get this simple data in Vaadin. – DarioBB Apr 29 '16 at 09:38
  • I have updated my question with code, can you help please? I have tried every single option trying to set default value on index 0 position – DarioBB Apr 29 '16 at 11:31
  • to me this looks like you are binding the null selection in. just put the default on the object you bind to. – cfrick Apr 29 '16 at 13:12
  • cfrick.. I do not understand. How I "just put" default.. ? – DarioBB Apr 29 '16 at 13:39

2 Answers2

1

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));
ripla
  • 422
  • 4
  • 12
0
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);
        }
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Rather than just throw some code up, you also should explain what the problem was and how you fixed it. This will make your answer more valuable to the original poster as well as future visitors. – miken32 Sep 06 '16 at 17:57
  • OK. I'll try. With this method You can select an item of the combobox at the index indicate as parameter. Thinked as combobox.selectedIndex(n) from Java Swing – frss-soft.com Sep 06 '16 at 22:06