Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Designer Components Data Binding using Annotation

Abhishek Goyal
7 years ago May 18, 2015 4:46pm
Petri Heinonen
7 years ago May 19, 2015 10:46pm

Hi Abhishek, 

As far as I know this is not (yet at least) possble because, as you noted, the Java file gets owerwritten when you change the design. 

However, there are a couple of ways to work around this: 

1) Name your fields in the designer so that they map 1:1 against the properties in your fieldGroup, then the fieldGroup will be able to find it. However, this will not work for nested properties. 
2) Extend the generated Java class and add a method that will manually bind a specific field to a specific property

For instance like this: 

public class MyBean {
        private String autoBinding = "";
        private String myCustomProperty = "";

       //Getters and setters omitted
    }
@DesignRoot public class MyDesign extends VerticalLayout {
        public TextField autoBinding;
        public TextField nonAutoBinding; 
        public MyDesign() {
            Design.read(this); 
        }
    }
public class MyForm extends MyDesign {
      
         public void bind(FieldGroup binder) {
            
            // Bind all the fields that map by name
            binder.bindMemberFields(this);

            //Manually bind the remaining fields, for instance neted properties. 
            binder.bind(nonAutoBinding, "myCustomProperty");
        }
    }

Then in your UI code: 

MyForm myForm = new MyForm();
        layout.addComponent(myForm);
        final BeanFieldGroup<MyBean> beanFieldGroup = new BeanFieldGroup<MyBean>(MyBean.class);
        beanFieldGroup.setItemDataSource(new MyBean());
        myForm.bind(beanFieldGroup);

Hope this helps

Best, 

Edit: added MyBean for reference.
 

Abhishek Goyal
7 years ago May 20, 2015 6:22am