How to make NativeSelect not show empty value

Here’s my code

NativeSelect cars=new NativeSelect();
cars.setNullSelectionAllowed(false);
cars.addItems("Honda", "Toyota", "Nissan");

But I still see the empty value at the top. I don’t want that. I want it to default to “Honda” in the list.

How do I do it?

Thanks

I use 7.4.6

Hi, ensure you have a selection initially. Add this call to your code:

cars.setValue(“Honda”);

Thanks. It worked but it still have the null option value at the top. Is there a way to remove that null option value from the list completely?

Hi,

It shouldn’t be there if a) the value is not initially null b) null selectioction is not allowed. This is how it works by default in our sampler example as well:

http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/drop-down-menu

The code in the example is like this:

[code]
sample = new NativeSelect(“Select an option”);
for (int i = 0; i < 6; i++) {
sample.addItem(i);
sample.setItemCaption(i, "Option " + i);
}

sample.setNullSelectionAllowed(false);
sample.setValue(2);
sample.setImmediate(true);
[/code]It uses “IndexedContainer” by default so that is why setValue has weird integer value.

If you can’t make it work, would you share a more complete example of your code?

cheers,
matti

Here’s my code

NativeSelect statuses = new NativeSelect("Performance Status");
statuses.addItems("0", "1", "2", "3");
statuses.setNullSelectionAllowed(false);
statuses.setValue("2");
statuses.setImmediate(true);

The value “2” is selected for me and there is no empty selection available:

https://www.dropbox.com/s/a0lqtwbdlwsovs6/Screenshot%202015-05-18%2017.04.55.png?dl=0

On which browser our you seeing this issue?

cheers,
matti

Something is interesting here. I tried something new. I just added that NativeSelect component into a VerticalLayout in MyUI.java then it worked like you said.

But my original code is in a FormLayout and I use BeanFieldGroup (bindMemberFields method) to bind it in the MyUI.java and it did not work.

I use Firefox ESR 31.6.0

Hi,

If you are using BeanItemContainer the value must be the actually selected bean, not the text that is displayed as “item caption”. Make sure you refer with the same instance or have properly impemented equals/hashCode methods.

cheers,
matti

I think that’s the reason why. I did not implement equals/hashCode methods in my beans.

Thanks