添加新项目时是否可以对普通组合框进行排序? (vaadin)

is it possible to sort ordinary combobox when I add new item? (vaadin)

我有一个组合框.. 当我在那里添加一个新项目时,它必须在列表的顶部..怎么可能将这个项目添加到顶部?

或者,只是尝试在添加新项目时按字母顺序对列表进行排序?

1.

how is it possible to add this item on the top?

您可以提供自己的数据源并使用它始终在位置 0 插入项目,类似于:

    TextField input = new TextField("New person name");
    BeanItemContainer<Person> comboDataSource = new BeanItemContainer<>(Person.class);
    ComboBox combo = new ComboBox("Persons", comboDataSource);
    combo.setItemCaptionPropertyId("name");

    layout.addComponent(input);
    layout.addComponent(combo);
    layout.addComponent(new Button("Add", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent clickEvent) {
            comboDataSource.addItemAt(0, new Person(input.getValue()));
        }
    }));

2.

try to sort this list in alphabetical order when new item has been added

Call the sort(Object[] propertyId, boolean[] ascending) method on your container 添加项目后。