You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoGeneratingAFormBasedOnABeanVaadin6StyleForm.asciidoc 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ---
  2. title: Auto Generating A Form Based On A Bean Vaadin 6 Style Form
  3. order: 21
  4. layout: page
  5. ---
  6. [[auto-generating-a-form-based-on-a-bean-vaadin-6-style-form]]
  7. = Auto-generating a form based on a bean - Vaadin 6 style Form
  8. In Vaadin 6 it is easy to get a completely auto generated form based on
  9. a bean instance by creating a `BeanItem` and passing that to a Form. Using
  10. `FieldGroup` this requires a few extra lines as `FieldGroup` never adds
  11. fields automatically to any layout but instead gives that control to the
  12. developer.
  13. Given a bean such as this `Person`:
  14. [source,java]
  15. ....
  16. public class Person {
  17. private String firstName,lastName;
  18. private int age;
  19. // + setters and getters
  20. }
  21. ....
  22. You can auto create a form using FieldGroup as follows:
  23. [source,java]
  24. ....
  25. public class AutoGeneratedFormUI extends UI {
  26. @Override
  27. public void init(VaadinRequest request) {
  28. VerticalLayout layout = new VerticalLayout();
  29. setContent(layout);
  30. FieldGroup fieldGroup = new BeanFieldGroup<Person>(Person.class);
  31. // We need an item data source before we create the fields to be able to
  32. // find the properties, otherwise we have to specify them by hand
  33. fieldGroup.setItemDataSource(new BeanItem<Person>(new Person("John", "Doe", 34)));
  34. // Loop through the properties, build fields for them and add the fields
  35. // to this UI
  36. for (Object propertyId : fieldGroup.getUnboundPropertyIds()) {
  37. layout.addComponent(fieldGroup.buildAndBind(propertyId));
  38. }
  39. }
  40. }
  41. ....