Hi, I want to create a plain and simple “table-edit” form, i.e. a table with a list of beans on top, and below an edit form where the user can edit the bean he selected in the table.
For the data binding in the edit form, I want to use the fieldgroup-mechanism. The code for the edit form looks something like this (the sample bean has two fields, “id” and “name”):
public class EditForm extends CustomComponent {
private VerticalLayout formLayout;
private FieldGroup fieldGroup;
// fields "id" and "name"
@PropertyId("id")
private TextField idTextField;
@PropertyId("name")
private TextField nameTextField;
[...]
//creating fields
nameTextField = new TextField();
idTextField = new TextField();
formLayout.addComponent(idTextField);
formLayout.addComponent(nameTextField);
//bind fields
fieldGroup = new FieldGroup();
fieldGroup.bindMemberFields(this);
[...]
//loading new bean
public void setBean(Bean bean) {
fieldGroup.setItemDataSource(new BeanItem(bean));
}
}
Now initially everything works fine, table and edit form are being displayed. The problem is when the user clicks a line in the table, the edit form isn’t updated with the selected bean. At the table, I registered a valueChangeListener which calls the “setBean(…)” method above, and this is being executed. However, although I do change the fieldgroup’s datasource to the new selected bean, the textfields in the UI simply don’t update. I noticed that only when setting the fieldGroup’s datasource immediately during creation of the edit form (e.g. directly after creating the textfields), the textfields are being filled with the bean’s values.
Not sure how you implemented the valuechangelistener, however the address book tutorial (
https://vaadin.com/tutorial ) does not seem to create a new Bean to bind form items to table.
Thank you for your answers, Oscar and gon gon. I tried both of your suggestions but with no success.
The valueChangeListener works fine, and it calls the setBean(…) method with the selected bean. I changed it to pass the entire BeanItem, as in the tutorial example, but it doesn’t help.
I found out that even setting a textfield invisible in the setBean(…) method has no effect. So it seems there’s something wrong with the update of the UI itself. Strangely, I can set the whole EditForm invisible there, but not a textfield within. Maybe this rings some solution with you.