How to populate form with data from file?

Hi guys. I’m trying to understand the data binding and stuck with populating FieldGroup with data from file.

I can receive all the needed data from properties file via neighbor module’s class. So I created Data and DataContainer classes. DataContainer extends BeanItemContainer. Data contains fields that should be populated with data from properties file.

But that’s all I understand :frowning: I added module’s class field to Data, but cannot understand how to use and how to bind that all together.

Are there any extended examples on data binding with different data sources? Or maybe you can guide me a little on where to move further?

Thank you a lot in advance.

Hi,

I understood that you’re reading just one bean from a properties file - in that case you should probably use just BeanItem, not BeamItemContainer, which is for multiple items.

How you read data to your beans is not really related to Vaadin.

Hm. I have several properties, let’s name them: property1, property2 and property3. And I have 3 TextFields: textField_prop1, textField_prop2 and textField_prop3 and described them as String in Data class. Getters/setter in this class will call module’s class for retrieving/saving data in properties file.
Also I created DataContainer that now will extend BeanItem. But I still cannot understand how to bind data to text fields :frowning:

Here is what I have:
Data

public class Data implements Serializable {
    private final transient Config config = Config.getInstance();

    private String textField_prop1 = "";
    private String textField_prop2 = "";
    private String textField_prop3 = "";

    public String getTextField_prop1() {
        return config.getProp1();
    }

    public void setTextField_prop1(String prop1) throws ConfigException {
        config.setProp1(prop1);
    }

    //Other getters and setters
}

DataContainer

public class DataContainer extends BeanItem<Data> implements Serializable {

    public DataContainer(Data bean) {
        super(bean);
    }
}

Form

public class TextFieldsForm extends Panel {
    private TextField textField_prop1 = new TextField("Prop1: ");
    private TextField textField_prop2 = new TextField("Prop2: ");
    private TextField textField_prop3 = new TextField("Prop3: ");

    public TextFieldsForm() {
        setCaption("Text Fields Properties:");
        setSizeFull();

        FormLayout formLayout = new FormLayout();
        formLayout.setHeight(300, Unit.PIXELS);
        formLayout.setMargin(true);
        formLayout.setSpacing(true);
        setContent(formLayout);

        //Add all components
        VerticalLayout content = new VerticalLayout();
        content.setSizeFull();
        content.addComponent(textField_prop1);
        content.addComponent(textField_prop2);
        content.addComponent(textField_prop3);

        setContent(content);
    }
}


updated

It seems it started to work :slight_smile: I wasn’t able to construct FieldGroup, but set data in text fields one by one works great.