如何在 Vaadin8 组合框中获取项目大小?

How to get item size in Vaadin8 combobox?

如果 Vaadin8 组合框中没有任何项目,我会尝试显示一个弹出窗口。但是没有 getItems() 或 size() 方法。

这是我的代码,如果分支大小 = 0,我想向用户推送通知。

        cbxBranch = new ComboBox<>();
        cbxBranch.setPlaceholder("Select a branch");
        cbxBranch.setItemCaptionGenerator(Branch::getBranchName);
        cbxBranch.setEmptySelectionAllowed(false);
        cbxBranch.setItems(getBranches());
        cbxBranch.addFocusListener(e -> {
            //this line just a sample..
            System.out.println(cbxBranch.getDataProvider().size());
        });

更新:

cbxBranch.addFocusListener(e -> {
    if (((ListDataProvider<Branch>) cbxBranch.getDataProvider()).getItems().isEmpty()) {
       Notification.show("You don't have a branch!", Type.WARNING_MESSAGE);
    }
});

Vaadin 8 使用 DataProviders 作为项目组件,例如 GridTreeGridComboBoxsetItems 方法是一种使用 array/collection 将 ListDataProvider 设置到组合框的便捷方法。因此,您可以调用 getDataProvider,转换为 ListDataProvider 并调用 getItems(参见 java 文档 here)。