如何将复选框绑定到布尔值 属性?
How to bind checkBox to a boolean property?
我最近开始使用vaadin框架。
我有一个包含一些文本字段和一个复选框的表单,我的问题是所有字段都与其属性绑定,期望复选框??。
这是表格:
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
...
}
我做错了什么?我应该手动绑定复选框字段吗?
可以得到check box
值onlistenter
的值,显式设置对象。
bookMarks.addListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
contact.setBookMarks(checkbox1.getValue());
}
});
要了解有关复选框的更多信息,请检查此 link
解决方案是像这样将addValueChangeListener
添加到复选框:
bookMarks.addValueChangeListener(event -> contact.setBookMarks(bookMarks.getValue()));
我认为您需要使用您创建的 formFieldBindings BeanFieldGroup。所以改变这一行
formFieldBindings = BeanFieldGroup.bindFieldsBuffered(contact, this);
至
formFieldBindings = new BeanFieldGroup<Contact>(Contact.class);
formFieldBindings.setItemDataSource(contact);
formFieldBindings.buildAndBindMemberFields(this);
我最近开始使用vaadin框架。
我有一个包含一些文本字段和一个复选框的表单,我的问题是所有字段都与其属性绑定,期望复选框??。
这是表格:
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
...
}
我做错了什么?我应该手动绑定复选框字段吗?
可以得到check box
值onlistenter
的值,显式设置对象。
bookMarks.addListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
contact.setBookMarks(checkbox1.getValue());
}
});
要了解有关复选框的更多信息,请检查此 link
解决方案是像这样将addValueChangeListener
添加到复选框:
bookMarks.addValueChangeListener(event -> contact.setBookMarks(bookMarks.getValue()));
我认为您需要使用您创建的 formFieldBindings BeanFieldGroup。所以改变这一行
formFieldBindings = BeanFieldGroup.bindFieldsBuffered(contact, this);
至
formFieldBindings = new BeanFieldGroup<Contact>(Contact.class);
formFieldBindings.setItemDataSource(contact);
formFieldBindings.buildAndBindMemberFields(this);