I am new to vaading and trying to understand and learn. I am working on a binder and trying to understand what happens
when we call discard method.
In my scenario, I am binding the fields through binder.bind(persontxt,“person”), I have all the form fields bounded and when i do discard only
few fields are getting discarded. I want to know how this happens and how shall I not discard particular fields?
Hi Olli,
I am using vaadin 7, here is a sample code.when i click on discard I am expecting it to clearing the text fields.
Which is not happening. I want to understand what happens when a discard method called.
public class BinderView extends FormLayout implements View {
private static final long serialVersionUID = -4490987023164596949L;
public BinderView() {
// Have some layout and create the fields
FormLayout form = new FormLayout();
TextField nameField = new TextField("Name");
form.addComponent(nameField);
TextField ageField = new TextField("Age");
form.addComponent(ageField);
// Now create the binder and bind the fields
//Bean is a java pojo class containing a string and double variable
Bean myBeanClass = new Bean("prajna",29.11);
BeanItem<Bean> item = new BeanItem<>(myBeanClass);
FieldGroup binder = new FieldGroup(item);
binder.setBuffered(true);
binder.bind(nameField, "name");
binder.bind(ageField, "energy");
Button refreshBtn = new Button("Refresh", new Button.ClickListener() {
private static final long serialVersionUID = 2251260916396644988L;
@Override
public void buttonClick(ClickEvent event) {
//clear the text fields
binder.discard();
}
});
form.addComponent(refreshBtn);
addComponent(form);
}
@Override
public void enter(ViewChangeEvent event) {
}
}
Okay, you’re talking about FieldGroup? binder is just the name of a variable in your own code. What FieldGroup.discard does is call discard on every Field connected to it. The behavior from the Field’s discard method can be seen from its JavaDoc:
/**
* Discards all changes since last commit. The object updates its value from
* the data source.
*
* @throws SourceException
* if the operation fails because of an exception is thrown by
* the data source. The cause is included in the exception.
*/
public void discard() throws SourceException;
So discarding doesn’t clear a TextField, it reverts any Field back to the value it has in the data source (in your case, the Bean myBeanClass).
PS. Vaadin 7 has not been publicly maintained for a few years now; you should look into migrating to a newer version.