gpt4 book ai didi

vaadin - 如何在 vaadin 流中绑定(bind)枚举值?

转载 作者:行者123 更新时间:2023-12-04 00:27:18 28 4
gpt4 key购买 nike

在我使用 springboot 的 vaadin 流程项目中,我遇到了无法绑定(bind)带有组合框的枚举值的问题。我在下面提供了我的代码。这里有谁能帮帮我吗?

组合框实例化:

private ComboBox<Country> nationality = new CompoBox<>("Nationality");

绑定(bind)代码:

binder.forField(nationality)
.bind(Coach::getNationality,Coach:setNationality);

最佳答案

我认为您缺少 setItems 调用。这是一个基于 Project Base for Vaadin Flow 的示例

package com.vaadin.starter.skeleton;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

/**
* The main view contains a button and a click listener.
*/
@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {

public MainView() {

ComboBox<ENUM> comboBox = new ComboBox<ENUM>("Number");
comboBox.setItems(ENUM.values());
Binder<Bean> binder = new Binder<>();
binder.setBean(new Bean());
binder.bind(comboBox,Bean::getField,Bean::setField);
Button button = new Button("Check value",
e-> Notification.show("Value in bean is " + binder.getBean().getField()));
add(button,comboBox);
}

public enum ENUM {
ONE,TWO,TREE
}

public static class Bean {
private ENUM field;

public ENUM getField() {

return field;
}

public void setField(ENUM field) {

this.field = field;
}
}
}

关于vaadin - 如何在 vaadin 流中绑定(bind)枚举值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193482/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com