Creating a CustomField for editing the address of a person

A normal use case is that you want to create a form out a bean that the user can edit. Often these beans contain references to other beans as well, and you have to create a separate editor for those. This tutorial goes through on how to edit an Address bean which is inside a Person bean with the use of CustomField and FieldGroup.

Here are the Person and Address beans

Java

  1. public class Person {
  2. private String firstName;
  3. private String lastName;
  4. private Address address;
  5. private String phoneNumber;
  6. private String email;
  7. private Date dateOfBirth;
  8. private String comments;
  9. //Getters and setters
  10. }

Java

  1. public class Address {
  2. private String street;
  3. private String zip;
  4. private String city;
  5. private String country;
  6. // Getters and setters
  7. }

Creating a new field

The first step is to create a new field which represents the editor for the address. In this case the field itself will be a button. The button will open a window where you have all the address fields. The address will be stored back when the user closes the window.

Java

  1. public class AddressPopup extends CustomField<Address> {
  2. @Override
  3. protected Component initContent() {
  4. return null;
  5. }
  6. @Override
  7. public Class<Address> getType() {
  8. return Address.class;
  9. }
  10. }

CustomField requires that you implement two methods, initContent() and getType(). initContent() creates the actual visual representation of your field. getType() tells the field which type of data will be handled by the field. In our case it is an Address object so we return Address.class in the method.

Creating the content

Next up we create the actual button that will be visible in the person editor when the CustomField is rendered. This button should open up a new window where the user can edit the address.

Java

  1. @Override
  2. protected Component initContent() {
  3. final Window window = new Window("Edit address");
  4. final Button button = new Button("Open address editor", new ClickListener() {
  5. public void buttonClick(ClickEvent event) {
  6. getUI().addWindow(window);
  7. }
  8. });
  9. return button;
  10. }

This is enough to attach the field to the person editor, but the window will be empty and it won’t modify the data in any way.

Creating the editable fields

The address object contains four strings - street, zip, city and country. For the three latter a TextField is good for editing, but the street address can contain multiple row so a TextArea is better here. All the fields have to be put into a layout and the layout has to be set as the content of the window. FormLayout is a good choice here to nicely align up the captions and fields of the window.

Java

  1. FormLayout layout = new FormLayout();
  2. TextArea street = new TextArea("Street address:");
  3. TextField zip = new TextField("Zip code:");
  4. TextField city = new TextField("City:");
  5. TextField country = new TextField("Country:");
  6. layout.addComponent(street);
  7. layout.addComponent(zip);
  8. layout.addComponent(city);
  9. layout.addComponent(country);
  10. window.setContent(layout);

The field is now visually ready but it doesn’t contain or affect any data. You want to also modify the sizes as well to make it look a bit nicer:

Java

  1. window.center();
  2. window.setWidth(null);
  3. layout.setWidth(null);
  4. layout.setMargin(true);

Binding the address to the field

A FieldGroup can be used to bind the data of an Address bean into the fields. We create a member variable for a FieldGroup and initialize it within the createContent() -method:

Java

  1. fieldGroup = new BeanFieldGroup<Address>(Address.class);
  2. fieldGroup.bind(street, "street");
  3. fieldGroup.bind(zip, "zip");
  4. fieldGroup.bind(city, "city");
  5. fieldGroup.bind(country, "country");

The FieldGroup of the person editor will call AddressPopup.setValue(person.getAddress()) when we start to edit our person. We need to override setInternalValue(Address) to get the Address object and pass it to the FieldGroup of the address editor.

Java

  1. @Override
  2. protected void setInternalValue(Address address) {
  3. super.setInternalValue(address);
  4. fieldGroup.setItemDataSource(new BeanItem<Address>(address));
  5. }

The last thing that has to be done is save the modifications made by the user back into the Address bean. This is done with a commit() call to the FieldGroup, which can be made for example when the window is closed:

Java

  1. window.addCloseListener(new CloseListener() {
  2. public void windowClose(CloseEvent e) {
  3. try {
  4. fieldGroup.commit();
  5. } catch (CommitException ex) {
  6. ex.printStackTrace();
  7. }
  8. }
  9. });

Now you need to attach the AddressPopup custom field into the person editor through it’s FieldGroup and you have a working editor.

Complete code

Java

  1. package com.example.addressforms.fields;
  2. import com.example.addressforms.data.Address;
  3. import com.vaadin.data.fieldgroup.BeanFieldGroup;
  4. import com.vaadin.data.fieldgroup.FieldGroup;
  5. import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
  6. import com.vaadin.data.util.BeanItem;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.Button.ClickEvent;
  9. import com.vaadin.ui.Button.ClickListener;
  10. import com.vaadin.ui.Component;
  11. import com.vaadin.ui.CustomField;
  12. import com.vaadin.ui.FormLayout;
  13. import com.vaadin.ui.TextArea;
  14. import com.vaadin.ui.TextField;
  15. import com.vaadin.ui.Window;
  16. import com.vaadin.ui.Window.CloseEvent;
  17. import com.vaadin.ui.Window.CloseListener;
  18. public class AddressPopup extends CustomField<Address> {
  19. private FieldGroup fieldGroup;
  20. @Override
  21. protected Component initContent() {
  22. FormLayout layout = new FormLayout();
  23. final Window window = new Window("Edit address", layout);
  24. TextArea street = new TextArea("Street address:");
  25. TextField zip = new TextField("Zip code:");
  26. TextField city = new TextField("City:");
  27. TextField country = new TextField("Country:");
  28. layout.addComponent(street);
  29. layout.addComponent(zip);
  30. layout.addComponent(city);
  31. layout.addComponent(country);
  32. fieldGroup = new BeanFieldGroup<Address>(Address.class);
  33. fieldGroup.bind(street, "street");
  34. fieldGroup.bind(zip, "zip");
  35. fieldGroup.bind(city, "city");
  36. fieldGroup.bind(country, "country");
  37. Button button = new Button("Open address editor", new ClickListener() {
  38. public void buttonClick(ClickEvent event) {
  39. getUI().addWindow(window);
  40. }
  41. });
  42. window.addCloseListener(new CloseListener() {
  43. public void windowClose(CloseEvent e) {
  44. try {
  45. fieldGroup.commit();
  46. } catch (CommitException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. });
  51. window.center();
  52. window.setWidth(null);
  53. layout.setWidth(null);
  54. layout.setMargin(true);
  55. return button;
  56. }
  57. @Override
  58. public Class<Address> getType() {
  59. return Address.class;
  60. }
  61. @Override
  62. protected void setInternalValue(Address address) {
  63. super.setInternalValue(address);
  64. fieldGroup.setItemDataSource(new BeanItem<Address>(address));
  65. }
  66. }

Address editor

Address editor window