Search code examples
javacheckboxvaadinvaadin7

How to bind checkBox to a boolean property?


I started using vaadin framework recently.

I have a Form which contains some textFields and a checkbox, my problem is all fields are binding with their properties expect checkbox ??.

This is the form :

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();
}}

This is My class Contact :

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
...
}

What am i doing wrong? Should I bind the checkbox field manually ?


Solution

  • The solution is to add addValueChangeListener to the check box like this :

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