Search code examples
vaadinvaadin7

vaadin, allow new typed item in combobox


I've faced with the issue with Vaadin's Combobox. I'd like to allow the user be able as select existed item from the list same provide his own value typing in the text field. I thought that it has to be easy, but... What I have now is

ComboBox roles = new ComboBox();
roles.setInputPrompt("Select Role");
roles.addItems(userService.getAllRoles());
roles.setImmediate(true);
roles.setNullSelectionAllowed(false);
roles.setNewItemsAllowed(true);
formLayout.addComponent(roles);

Here I've found that setNewItemsAllowed allows such behavior, but for some reason it doesn't work for me. When I start typing some new value I can see an empty drop-down and when I select another field the value in checkbox reverts to prompt text.


Solution

  • It is not enough to allow new items in the ComboBox. You must alos set the new item handler, until you do so, the ComboBox has no way to know what a new item should look like.

    roles.setNewItemHandler(....your handler....);
    

    Example code and the Docu for it.