Combobox looses datasource after first selection

Hello,

I’ve a combobox which has a SQLContainer as its datasource. Everything is fine until one wants to select another item after he has already selected one.

Ok, step by step:

  1. The combobox is rendered and provides a list with the appropriated items from the containersource

    The user selects an item

    If you want to open the combobox again, you’ll only see a single entry now (however, I guess this point is quite normal because the list is filtered for that particular item)

    If you clear the combobox you can’t get the list again


Why does the combobox loose its datascource after the first selection?

Thanks and regards

It doesn’t, by default. What are you doing in the value change listener?

Nothing really special… Just a bunch of setters.

        combo2.addValueChangeListener(new ValueChangeListener(){
            @Override
            public void valueChange(ValueChangeEvent event) {
                
                String value = combo2.getItemCaption(combo2.getValue());
                CostCentre centre = new CostCentre((value != null) ? Integer.valueOf(value) : 0);
                
                if(centre.getDescription() != null) {
                    tfName.setValue(centre.getDescription());
                } else {
                    tfName.setValue(null);
                }
                
                if(centre.getLocation() != null) {
                    tfLocation.setValue(centre.getLocation());
                } else {
                    tfLocation.setValue(null);
                }
            }
        });

Yeah, nothing strange there. What about the filtering? you mention this:
“If you want to open the combobox again, you’ll only see a single entry now (however, I guess this point is quite normal because the list is filtered for that particular item)”

Do you remove the filters you add at any point?

Nah I was just talking about the filter which is provided out of the box (the “you type and shorten the list”-thing). I don’t manipulate this feature in any way.

that feature doesn’t engage when an item is selected; it is only if you’d type into the combobox. See e.g. the sampler:
http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/combo-box
. So, you should be seeing the complete list at that point.

Okay, thank you for that input! :slight_smile:
However, this makes this problem even more weird, doesn’t it?

One more thing: I don’t use the vanilla combobox. I’ve added two methods in a child class:

public class CostCentreComboBox extends ComboBox {
    
    public CostCentreComboBox() throws SQLException {
        super();
        init();
    }

    public CostCentreComboBox(String caption, Collection<?> options) throws SQLException {
        super(caption, options);
        init();
    }

    public CostCentreComboBox(String caption, Container dataSource) throws SQLException {
        super(caption, dataSource);
        init();
    }

    public CostCentreComboBox(String caption) throws SQLException {
        super(caption);
        init();
    }
    
    private void init() throws SQLException {
        this.setContainerDataSource(generateContainer());
        this.setItemCaptionPropertyId("SECRET"); //$NON-NLS-1$
    }
    
    private SQLContainer generateContainer() throws SQLException {
        return ((MyUI) UI.getCurrent()).getContainer().getContainerByQueryName("SECRET"); //$NON-NLS-1$
    }
    
}

So you store the container reference in the UI. This is OK, but do you use the same container somewhere else? Some feature are container and not component specific e.g. filtering. So if two components use the same container, and one component filters it, the filtering will apply to the second component too.

Yeah, I’ve taken care of this. Also note, that I provide a unfiltered container (the combobox gets “the whole table”) and that the described usecases’ steps follow immediately after each other (and I don’t take any actions synchronously).

Can you try the exact same use case, but have a separate conatiner just for this combobox? that way we can be sure that the sharing doesn’t impact the behaviour.

:slight_smile: We got it!

I created the SQLContainer in the common “single instance” way and voilà :slight_smile:
I’m going to test a few things out and post some code here.


Thanks Thomas!

You’re very welcome :slight_smile: