How to do Reset functionality in Vaadin?

Hi Vaadin Experts,

     I have developed a View Page with some text fields, combo box, Submit and Reset button. This page is automatically loaded with user data available in DB. Whenever the user is modifying some data and clicking the submit button then data available in the page has to be updated to DB. My issue is whenever the user is clicking the Reset button the data should be reverted to the default values (as loaded before). Is there any way to acheive the Reset functionality other than setting values for all the fields to default in the button click listener?

Saravanan B

Fields implementing the Buffered interface (wich are most if not all of the provided ones) can be reset by calling the discard() method. So if your user clicks the reset button, you just loop through the fields and call discard() on each. The fields will be reset to the value of the underlying property.

Hi Werner,

      Thank you so much for your valid inputs. Please find the attached sample program to test the reset functionality as stated above. Its not working. Please correct me if i am wrong or missed anything. (1.58 KB)

Ah yes. I just ran into this issue myself. You may have to call setBuffered(true) first before buffering is actually enabled.

Hi Werner,

      I have tried to set the buffered for all the fields as below,


Now also i am not able to reset the fields to default value by calling the dicard() function.

Please suggest.

Ok, I got it now. I rarely use fields without Properties so I didn’t notice the problem at first. You actually have to have a Property that encapsulates the value. In your case you could use a simple Item for demonstration purposes

Item i = new PropertysetItem(); i.addItemProperty("name", new ObjectProperty<String>(null, String.class)); i.addItemProperty("password", new ObjectProperty<String>(null, String.class)); i.addItemProperty("gender", new ObjectProperty<String>(null, String.class)); or something more high level like the BeanItem:

public static class User { private String name; private String password; private String gender; // getters and setters } and later in your code

User user = new User(); Item i = new BeanItem<User>(user); Regardless of which method you choose, you associate the properties with your fields like so:

final TextField userNameField = new TextField("User Name", i.getItemProperty("name"));
// ...
final PasswordField passField = new PasswordField("Password", i.getItemProperty("password"));
// ...

I hope this works now. :slight_smile:

Its working perfectly Werner :). Thank you so much for your inputs. Really you are awesome :).