vaadin,允许在组合框中输入新的项目

vaadin, allow new typed item in combobox

我遇到过 Vaasin 组合框的问题。我想让用户能够像列表中的 select 项一样在文本字段中输入自己的值。我认为它必须很容易,但是......我现在拥有的是

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

在这里我发现 setNewItemsAllowed 允许这样的行为,但出于某种原因它对我不起作用。当我开始输入一些新值时,我可以看到一个空的下拉列表,当我 select 另一个字段时,复选框中的值恢复为提示文本。

在 ComboBox 中允许新项目是不够的。 您还必须设置新的项目处理程序,否则 ComboBox 无法知道新项目的外观。

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

Example code and the Docu for it.