Creating a master details view for editing persons

Set-up

In this tutorial we go through a standard use case where you have a bean and a backend ready with create, read, update and delete capabilities on that bean. You want to create a view where you can view all the beans and edit them. This example is an address book where you edit person information. The bean and the backend that we’re going to use looks like this:

Person

Java

  1. public class Person {
  2. private int id = -1;
  3. private String firstName = "";
  4. private String lastName = "";
  5. private Address address = new Address();
  6. private String phoneNumber = "";
  7. private String email = "";
  8. private Date dateOfBirth = null;
  9. private String comments = "";

IBackend

Java

  1. public interface IBackend {
  2. public List<Person> getPersons();
  3. public void storePerson(Person person);
  4. public void deletePerson(Person person);
  5. }

The view will contain a table, with all the persons in it, and a form for editing a single person. Additionally the table will have buttons too add or remove persons and the form will have buttons to save and discard changes. The UI wireframe looks like this:

Master detail UI wireframe

Building the basic UI

We start off with creating a basic UIfor our application

Java

  1. public class AddressFormsUI extends UI {
  2. @Override
  3. protected void init(VaadinRequest request) {
  4. VerticalLayout mainLayout = new VerticalLayout();
  5. mainLayout.setSpacing(true);
  6. mainLayout.setMargin(true);
  7. mainLayout.addComponent(new Label("Hello Vaadiners!"));
  8. setContent(mainLayout);
  9. }
  10. }

The first things that we want to add to it is the table and the form. The table should be selectable and immediate so that we’re able to pass person objects from it to the form. I will create all the fields for our person editor by hand to get more flexibility in how the fields will be built and laid out. You could also let Vaadin FieldGroup take care of creating the standard fields with the buildAndBind -methods if you don’t need to customize them.

Java

  1. package com.example.addressforms;
  2. import com.vaadin.server.VaadinRequest;
  3. import com.vaadin.ui.Component;
  4. import com.vaadin.ui.DateField;
  5. import com.vaadin.ui.GridLayout;
  6. import com.vaadin.ui.UI;
  7. import com.vaadin.ui.Table;
  8. import com.vaadin.ui.TextArea;
  9. import com.vaadin.ui.TextField;
  10. import com.vaadin.ui.VerticalLayout;
  11. public class AddressFormsUI extends UI {
  12. private GridLayout form;
  13. private Table table;
  14. @Override
  15. protected void init(VaadinRequest request) {
  16. VerticalLayout mainLayout = new VerticalLayout();
  17. mainLayout.setSpacing(true);
  18. mainLayout.setMargin(true);
  19. mainLayout.addComponent(buildTable());
  20. mainLayout.addComponent(buildForm());
  21. setContent(mainLayout);
  22. }
  23. private Component buildTable() {
  24. table = new Table(null);
  25. table.setWidth("500px");
  26. table.setSelectable(true);
  27. table.setImmediate(true);
  28. return table;
  29. }
  30. private Component buildForm() {
  31. form = new GridLayout(2, 3);
  32. TextField firstName = new TextField("First name:");
  33. TextField lastName = new TextField("Last name:");
  34. TextField phoneNumber = new TextField("Phone Number:");
  35. TextField email = new TextField("E-mail address:");
  36. DateField dateOfBirth = new DateField("Date of birth:");
  37. TextArea comments = new TextArea("Comments:");
  38. form.addComponent(firstName);
  39. form.addComponent(lastName);
  40. form.addComponent(phoneNumber);
  41. form.addComponent(email);
  42. form.addComponent(dateOfBirth);
  43. form.addComponent(comments);
  44. return form;
  45. }
  46. }

Address form

We also want the add, remove, save and discard buttons so let’s create them as well. I’ve positioned the add and remove above the table and save and discard below the form.

Java

  1. private GridLayout form;
  2. private HorizontalLayout tableControls;
  3. private Table table;
  4. private HorizontalLayout formControls;
  5. @Override
  6. protected void init(VaadinRequest request) {
  7. VerticalLayout mainLayout = new VerticalLayout();
  8. mainLayout.setSpacing(true);
  9. mainLayout.setMargin(true);
  10. mainLayout.addComponent(buildTableControls());
  11. mainLayout.addComponent(buildTable());
  12. mainLayout.addComponent(buildForm());
  13. mainLayout.addComponent(buildFormControls());
  14. setContent(mainLayout);
  15. }
  16. ...
  17. private Component buildTableControls() {
  18. tableControls = new HorizontalLayout();
  19. Button add = new Button("Add");
  20. Button delete = new Button("Delete");
  21. tableControls.addComponent(add);
  22. tableControls.addComponent(delete);
  23. return tableControls;
  24. }
  25. private Component buildFormControls() {
  26. formControls = new HorizontalLayout();
  27. Button save = new Button("Save");
  28. Button discard = new Button("Discard");
  29. formControls.addComponent(save);
  30. formControls.addComponent(discard);
  31. return formControls;
  32. }

The buttons doesn’t do anything yet but we have all the components that we need in the view now.

Address form with add

Connecting the backend to the view

The backend reference is store as a field so that all methods have access to it.

Java

  1. ...
  2. private IBackend backend;
  3. @Override
  4. protected void init(VaadinRequest request) {
  5. backend = new Backend();
  6. ...

Then we have to build a container for the table. I will do it in a separate method from the table building so that it can be rebuilt for refreshing the table after the initial rendering. We call this method once in the initial rendering as well on every button click that modifies the list of persons. A good choice of container in this case is the BeanItemContainer where we specify to the table which columns we want to show, and sort the table based on the name.

Java

  1. ...
  2. private Component buildTable() {
  3. table = new Table(null);
  4. table.setSelectable(true);
  5. table.setImmediate(true);
  6. updateTableData();
  7. return table;
  8. }
  9. ...
  10. private void updateTableData() {
  11. List<Person> persons = backend.getPersons();
  12. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  13. Person.class, persons);
  14. table.setContainerDataSource(container);
  15. table.setVisibleColumns(new String[] { "firstName", "lastName",
  16. "phoneNumber", "email", "dateOfBirth" });
  17. table.setColumnHeaders(new String[] { "First name", "Last name",
  18. "Phone number", "E-mail address", "Date of birth" });
  19. table.sort(new Object[] { "firstName", "lastName" }, new boolean[] {
  20. true, true });
  21. }
  22. ...

To get the data from the selected person’s data into the fields, and the changes back into the bean, we will use a FieldGroup. The FieldGroup should be defined as class variable and it should bind the fields that is initialized in buildForm().

Java

  1. ...
  2. private FieldGroup fieldGroup = new FieldGroup();
  3. ...
  4. private Component buildForm() {
  5. form = new GridLayout(2, 3);
  6. TextField firstName = new TextField("First name:");
  7. TextField lastName = new TextField("Last name:");
  8. TextField phoneNumber = new TextField("Phone Number:");
  9. TextField email = new TextField("E-mail address:");
  10. DateField dateOfBirth = new DateField("Date of birth:");
  11. TextArea comments = new TextArea("Comments:");
  12. fieldGroup.bind(firstName, "firstName");
  13. fieldGroup.bind(lastName, "lastName");
  14. fieldGroup.bind(phoneNumber, "phoneNumber");
  15. fieldGroup.bind(email, "email");
  16. fieldGroup.bind(dateOfBirth, "dateOfBirth");
  17. fieldGroup.bind(comments, "comments");
  18. form.addComponent(firstName);
  19. form.addComponent(lastName);
  20. form.addComponent(phoneNumber);
  21. form.addComponent(email);
  22. form.addComponent(dateOfBirth);
  23. form.addComponent(comments);
  24. return form;
  25. }

Additionally the table requires a value change listener and the currently selected person in the table has to be passed to the FieldGroup.

Java

  1. private Component buildTable() {
  2. ...
  3. table.addValueChangeListener(new ValueChangeListener() {
  4. public void valueChange(ValueChangeEvent event) {
  5. editPerson((Person) table.getValue());
  6. }
  7. });
  8. ...
  9. }
  10. ...
  11. private void editPerson(Person person) {
  12. if (person == null) {
  13. person = new Person();
  14. }
  15. BeanItem<Person> item = new BeanItem<Person>(person);
  16. fieldGroup.setItemDataSource(item);
  17. }

Putting the buttons in use

Last thing we have to do is implement all the buttons that we have in the application. Add should create a new Person object and give it to the form. Delete should tell the backend to remove the selected person and update the table. Save should store the changes into the bean and the bean into the backend and update the table. Discard should reset the form.

Java

  1. private Component buildTableControls() {
  2. tableControls = new HorizontalLayout();
  3. Button add = new Button("Add", new ClickListener() {
  4. public void buttonClick(ClickEvent event) {
  5. editPerson(new Person());
  6. }
  7. });
  8. Button delete = new Button("Delete", new ClickListener() {
  9. public void buttonClick(ClickEvent event) {
  10. backend.deletePerson((Person) table.getValue());
  11. updateTableData();
  12. }
  13. });
  14. tableControls.addComponent(add);
  15. tableControls.addComponent(delete);
  16. return tableControls;
  17. }
  18. private Component buildFormControls() {
  19. formControls = new HorizontalLayout();
  20. Button save = new Button("Save", new ClickListener() {
  21. public void buttonClick(ClickEvent event) {
  22. try {
  23. fieldGroup.commit();
  24. backend.storePerson(((BeanItem<Person>) fieldGroup
  25. .getItemDataSource()).getBean());
  26. updateTableData();
  27. editPerson(null);
  28. } catch (CommitException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. });
  33. Button discard = new Button("Discard", new ClickListener() {
  34. public void buttonClick(ClickEvent event) {
  35. fieldGroup.discard();
  36. }
  37. });
  38. formControls.addComponent(save);
  39. formControls.addComponent(discard);
  40. return formControls;
  41. }

Form with database connected

That’s it! Now you have a full working CRUD view with total control over the components and layouts. A little theming and layout adjustments and it is ready for production.

You might have noticed that the person bean contains a reference to another bean, a address, which is not editable here. The tutorial Creating a custom field for editing the address of a person goes through on how to edit beans within beans with a CustomField, which can be used directly as a field for the FieldGroup.