How to update data in datasource only on save button click?

I have several text fields describing some configuration. When I launch my app these fields are filled with data from config.properties file. I also have 3 buttons - edit, save and cancel. Edit activates save and cancel buttons, and also discards read only from text fields. If I change data in text fields and click cancel the data still saves and config.properties file is changed.

Here is my bean:

public class Config implements Serializable {
    private String address = "";
    private String uri = "";
    private String host = "";

    public String getAddress() {
        return ConfigUtil.getInstance().getAddress();
    }

    public void setAddress(String address) throws ConfigException {
        ConfigUtil.getInstance().setAddress(address);
    }
....

And container:

public class ConfigContainer extends BeanItem<Config> implements Serializable {

    public ConfigContainer(Config bean) {
        super(bean);
    }
}

And this is my UI:

public class ConfigSettings extends Panel {
    private TextField addressField = new TextField("Address: ");
    private TextField uriField = new TextField("URI: ");
    private TextField hostField = new TextField("Host: ");
 
    public ConfigSettings() {
         //add components
         ....

         //read data
        addressField.setPropertyDataSource(dataSource.getItemProperty("address"));
        uriField.setPropertyDataSource(dataSource.getItemProperty("uri"));
        hostField.setPropertyDataSource(dataSource.getItemProperty("host"));
    }

I don’t have setImmediate on any of components. And I even don’t have any save/cancel logic on buttons, but data is still saved :frowning:
What is wrong here and how to correct it?

addressField.setWriteThrough(false);
uriField.setWriteThrough(false);
hostField.setWriteThrough(false);

saleButton.addClickListener( new Button.ClickListener() {

addressField.commit();
uriField.commit();
hostField.commit();

} ) ;

Something along that line.

However , To ensure that data is never parially commited ( ie due to validation etc. ) you should decouple the Bean from the source.
The bean should be a simple POJO,
Read its values from the ConfigUtil.getInstance()
make a Form with your bean as Item.
only when form.commit() succeeds do you write the values of your bean to ConfigUtil.getInstance() …

Thanks, it helped me a lot. And as a result I reworked this class to generate form via FieldGroup. Now it saves only when commit is called :slight_smile: