如何从vaadin列表框中选择项目?

puruo6ea  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(687)

我正在尝试从vaadin listbox元素中选择一个项。我用数据库中对象的数组列表填充列表框。选择对象/列表项后,我想用所选对象的属性填充文本字段。这是我目前的代码。我´我试了那么多,但是我能´无法正常工作:/

// creating a ArrayList - listOfItems - filled with Items from the Database

        listBox.setItems(listOfItems);
        listBox.setHeight("100px");
        add(listBox);

        Div value = new Div();
        listBox.addValueChangeListener(event -> {
            if (event.getValue() == null) {
                Notification.show(event.getValue().toString());
            } else {
                Notification.show("value is null");
            }
        });

有人知道为什么吗?
提前谢谢

uqxowvwt

uqxowvwt1#

你可以用 .setValue(...) listbox类的方法。但是,当您从数据库加载数据时,您必须确保您选择的项目与您通过提供的项目完全相同 .setItems(...) 方法。这意味着,其中一个提供的项必须与要选择的项具有完全相同的哈希代码。否则您的选择可能不起作用。
对于一些示例,请看:https://vaadin.com/components/vaadin-list-box/java-examples

cnjp1d6j

cnjp1d6j2#

这里有一个bug:

listBox.addValueChangeListener(event -> {
            if (event.getValue() == null) {
                Notification.show(event.getValue().toString());
            } else {
                Notification.show("value is null");
            }
        });

在第一部分 if 声明,你打电话来 event.getValue().toString() ,这将导致空指针异常,如 event.getValue() 为空。所以把条件转到 if (event.getValue() != null)

相关问题