Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Binding Components to Data

The above way of binding is not different from simply calling setPropertyDataSource() for the fields. It does, however, register the fields in the field group, which for example enables buffering or validation of the fields using the field group, as described in Section 9.4.4, “Buffering Forms”.

Next, we consider more practical uses for a FieldGroup.

9.4.2. Using a FieldFactory to Build and Bind Fields

Using the buildAndBind() methods, FieldGroup can create fields for you using a FieldGroupFieldFactory, but you still have to add them to the correct position in your layout.

// Have some layout

FormLayout form = new FormLayout();

//Now create a binder that can also create the fields

//using the default field factory

FieldGroup binder = new FieldGroup(item); form.addComponent(binder.buildAndBind("Name", "name")); form.addComponent(binder.buildAndBind("Age", "age"));

9.4.3. Binding Member Fields

The bindMemberFields() method in FieldGroup uses reflection to bind the properties of an item to field components that are member variables of a class. Hence, if you implement a form as a class with the fields stored as member variables, you can use this method to bind them supereasy.

The item properties are mapped to the members by the property ID and the name of the member variable. If you want to map a property with a different ID to a member, you can use the @PropertyId annotation for the member, with the property ID as the parameter.

For example:

// Have an item

PropertysetItem item = new PropertysetItem(); item.addItemProperty("name", new ObjectProperty<String>("Zaphod")); item.addItemProperty("age", new ObjectProperty<Integer>(42));

// Define a form as a class that extends some layout class MyForm extends FormLayout {

//Member that will bind to the "name" property TextField name = new TextField("Name");

//Member that will bind to the "age" property @PropertyId("age")

TextField ageField = new TextField("Age");

public MyForm() {

//Customize the layout a bit setSpacing(true);

//Add the fields addComponent(name); addComponent(ageField);

}

}

// Create one

MyForm form = new MyForm();

// Now create a binder that can also creates the fields

250

Using a FieldFactory to Build and Bind Fields

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]