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.

Dynamic Binding Using Bean from Arraylist

Ashwin Almeda
5 years ago May 18, 2016 11:24am

Hi There,

I am trying to build a dynamic form builder. 

My Field Wrapper Class is

public class FormField {    public enum TYPES {        DATE, TEXT, MEMO, COMBO, CHECKBOX    }

    private String name;
    private String id;
    private Object value;
    private boolean booleanValue;
    private TYPES type;

    

    public boolean isBooleanValue() {
        return booleanValue;
    }

    public void setBooleanValue(boolean booleanValue) {
        this.booleanValue = booleanValue;
    }

    public FormField(String name, String id, Object value, TYPES type) {
        super();
        this.name = name;
        this.id = id;
        this.value = value;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public TYPES getType() {
        return type;
    }

    public void setType(TYPES type) {
        this.type = type;
    }

}

----------------------------------------------

Then I Initiliase the fields in my DataSource Bean

public class MyDynamicDataSourceBean{        private List<FormField> formFields;

    public List<FormField> getFormFields() {
        return formFields;
    }

    public void setFormFields(List<FormField> formFields) {
        this.formFields = formFields;
    }

    public MyDynamicDataSourceBean() {
//Its hardcoded for now, but intention is to get the dynamic from external source and fill the metadata for fields
        FormField nameField = new FormField("Employee Name", "employeeName", null, FormField.TYPES.TEXT);
        FormField departmentField = new FormField("Employee Department", "employeeDept", null, FormField.TYPES.COMBO);
        FormField cityField = new FormField("City", "city", null, FormField.TYPES.TEXT);
        FormField dateOfBirthField = new FormField("Employee DOB", "employeeDOB", null, FormField.TYPES.DATE);
        FormField permanentEmpField = new FormField("Is Permanent Employee?", "permanentEmp", null,
                FormField.TYPES.TEXT);
        formFields = new ArrayList<FormField>();
        formFields.add(nameField);
        formFields.add(departmentField);
        formFields.add(cityField);
        formFields.add(dateOfBirthField);
        formFields.add(permanentEmpField);
    }

Then Use this in my View Impl

MultiColumnFormLayout multiColumnFormLayout = new MultiColumnFormLayout(2,                myDynamicDataSourceBean.getFormFields().size());        int index = 0;        for (FormField formField : datasourceBean.getFormFields()) {            if (FormField.TYPES.TEXT.equals(formField.getType())) {                TextField textField = new TextField(caption);                textField.setImmediate(true);                fieldGroup.bind(textField, "formFields[" + index + "].value");                textField.setValidationVisible(false);                textField.setNullRepresentation(TEXT_NULL_REPRESENTATION);                multiColumnFormLayout.addComponent(textField);            } else if (FormField.TYPES.COMBO.equals(formField.getType())) {                ........            } else if (FormField.TYPES.DATE.equals(formField.getType())) {                .....            } else if (FormField.TYPES.MEMO.equals(formField.getType())) {

            } else if (FormField.TYPES.CHECKBOX.equals(formField.getType())) {
                ...........
            }
            index++;
        }

But when I run the form I get the following exception:

java.lang.IllegalArgumentException: Bean property 'formFields[0]' not found
    at com.vaadin.data.util.NestedMethodProperty.initialize(NestedMethodProperty.java:149)
    at com.vaadin.data.util.NestedMethodProperty.<init>(NestedMethodProperty.java:95)
    at com.vaadin.data.util.BeanItem.addNestedProperty(BeanItem.java:251)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.ensureNestedPropertyAdded(BeanFieldGroup.java:146)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:153)
    at com.vaadin.data.fieldgroup.FieldGroup.setItemDataSource(FieldGroup.java:105)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.setItemDataSource(BeanFieldGroup.java:125)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.setItemDataSource(BeanFieldGroup.java:118)
    at com.hrss.app.web.form.demo.view.BasicFormImpl.setDatasource(BasicFormImpl.java:321)
    at com.hrss.app.web.view.letter.LetterRequestView.showForm(LetterRequestView.java:170)
    at com.hrss.app.web.view.letter.LetterRequestView.access$0(LetterRequestView.java:162)
    at com.hrss.app.web.view.letter.LetterRequestView$1.layoutClick(LetterRequestView.java:149)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1008)
    at com.vaadin.ui.AbstractOrderedLayout.access$000(AbstractOrderedLayout.java:42)
    at com.vaadin.ui.AbstractOrderedLayout$1.layoutClick(AbstractOrderedLayout.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:408)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:273)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:79)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1409)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: com.mypackage.MyDynamicDataSourceBean.areFormFields[0]()
    at java.lang.Class.getMethod(Class.java:1786)
    at com.vaadin.data.util.MethodProperty.initGetterMethod(MethodProperty.java:545)
    at com.vaadin.data.util.NestedMethodProperty.initialize(NestedMethodProperty.java:144)
    ... 55 more

Any one has tried this?
 

Ladislav Jech
4 years ago Aug 30, 2017 11:31am
Ladislav Jech
4 years ago Aug 30, 2017 11:31am