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