Binding Data to Forms

In many applications users provide structured data by completing fields in forms. This data is typically represented in code as an instance of a business object (JavaBean), for example a Person in an HR application.

The Binder class allows you to define how the values in a business object are bound to fields in the UI.

Binder reads the values in the business object and converts them from the format expected by the business object to the format expected by the field, and vice versa.

Binder can only bind components that implement the HasValue interface, for example TextField and ComboBox.

It is also possible to validate user input and present the validation status to the user in different ways.

How to Bind Form Data

The following steps include everything needed to load, edit and save values for a form. Java 8 method references are used.

To bind data to a form:

  1. Create a Binder and bind the input fields.

    Note
    There can only be one Binder instance for each form. You should use it for all fields in the form.

    Java

    1. Binder<Person> binder = new Binder<>(Person.class);
    2. TextField titleField = new TextField();
    3. // Start by defining the Field instance to use
    4. binder.forField(titleField)
    5. // Finalize by doing the actual binding
    6. // to the Person class
    7. .bind(
    8. // Callback that loads the title
    9. // from a person instance
    10. Person::getTitle,
    11. // Callback that saves the title
    12. // in a person instance
    13. Person::setTitle);
    14. TextField nameField = new TextField();
    15. // Shorthand for cases without extra configuration
    16. binder.bind(nameField, Person::getName,
    17. Person::setName);
  2. Use the Binder to:

    1. Load values from a person into the field.

    2. Allow the user to edit the values.

    3. Save the values back into a person instance.

      Java

      1. // The person to edit
      2. // Would be loaded from the backend
      3. // in a real application
      4. Person person = new Person("John Doe", 1957);
      5. // Updates the value in each bound field component
      6. binder.readBean(person);
      7. Button saveButton = new Button("Save",
      8. event -> {
      9. try {
      10. binder.writeBean(person);
      11. // A real application would also save
      12. // the updated person
      13. // using the application's backend
      14. } catch (ValidationException e) {
      15. notifyValidationException(e);
      16. }
      17. });
      18. // Updates the fields again with the
      19. // previously saved values
      20. Button resetButton = new Button("Reset",
      21. event -> binder.readBean(person));
      • Every time writeBean is called, the data is validated and then copied from the UI to the business object.

      • If the data is invalid, a ValidationException that includes all errors in the data, is thrown. This is the reason writeBean is in a try/catch block.

It is also possible to use a Lambda expression, instead of a method reference.

Java

  1. // With lambda expressions
  2. binder.bind(titleField,
  3. person -> person.getTitle(),
  4. (person, title) -> {
  5. person.setTitle(title);
  6. logger.info("setTitle: {}", title);
  7. });

Binding Read-only Data

To bind a component to read-only data, you can use a null value for the setter.

Example: Using a null value setter.

Java

  1. TextField fullName = new TextField();
  2. binder.forField(fullName)
  3. .bind(Person::getFullName, null);

To bind components that do not implement the HasValue interface to read-only data, you can use the ReadOnlyHasValue helper class.

Example: Using the ReadOnlyHasValue helper class.

Java

  1. Label fullNameLabel = new Label();
  2. ReadOnlyHasValue<String> fullName =
  3. new ReadOnlyHasValue<>(
  4. text -> fullNameLabel.setText(text));
  5. binder.forField(fullName)
  6. .bind(Person::getFullName, null);