gpt4 book ai didi

java - 如何将 checkBox 绑定(bind)到 boolean 属性?

转载 作者:行者123 更新时间:2023-12-01 20:20:08 25 4
gpt4 key购买 nike

我最近开始使用vaadin框架。

我有一个包含一些文本字段和一个复选框的表单,我的问题是所有字段都与其属性绑定(bind),除了复选框??。

这是表格:

public class ContactForm extends FormLayout {

private Button save = new Button("Save", this::save);
private Button delete = new Button("Delete", this::delete);
private Button cancel = new Button("Cancel", this::cancel);
private TextField firstName = new TextField("First name");
private TextField lastName = new TextField("Last name");
private TextField phone = new TextField("Phone");
private TextField email = new TextField("Email");
private DateField birthDate = new DateField("Birth date");
private CheckBox bookMarks = new CheckBox("BookMarks");

private Contact contact;

// Easily bind forms to beans and manage validation and buffering
private BeanFieldGroup<Contact> formFieldBindings;

public ContactForm() {
configureComponents();
buildLayout();
}

private void configureComponents() {...}

private void buildLayout() {...}



void edit(Contact contact) {

this.contact = contact;
if (contact != null) {
// Bind the properties of the contact POJO to fields in this form
formFieldBindings = BeanFieldGroup.bindFieldsBuffered(contact, this);

delete.setVisible(contact.getId() != null);
}

setVisible(contact != null);
}

@Override
public AddressbookUI getUI() {
return (AddressbookUI) super.getUI();
}}

这是我的类(class)联系人:

public class Contact implements Serializable, Cloneable {

private Long id;

private String firstName = "";
private String lastName = "";
private String phone = "";
private String email = "";
private Date birthDate;
private Boolean bookMarks;

// getters and setters
...
}

我做错了什么?我应该手动绑定(bind)复选框字段吗?

最佳答案

解决方案是将 addValueChangeListener 添加到复选框,如下所示:

bookMarks.addValueChangeListener(event -> contact.setBookMarks(bookMarks.getValue()));

关于java - 如何将 checkBox 绑定(bind)到 boolean 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44947365/

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