Modifying the bean Item's source for a form

I have an Employee Form like this


public EmployeeForm() {
        setFormFieldFactory(new FormFieldFactory() {

            @Override
            public Field createField(Item item, Object propertyId,
                    Component uiContext) {
                String pid = (String) propertyId;
                System.out.println(pid);

                Field field1 = DefaultFieldFactory.get().createField(item,
                        propertyId, uiContext);

                if (pid.equals("firstName")) {
                    TextField field = new TextField("First name");
                    field.setRequired(true);
                    field.setRequiredError("First name cannot be empty");
                    field.setNullRepresentation("");
                    return field;
                } else if (pid.equals("address")) {
                    return new AddressForm();
                }

                return null;
            }
        });

        setItemDataSource(new BeanItem<Employee>(new Employee()));
        setVisibleItemProperties(new String[] { "firstName", "address" });
    }
}

When i wanna display the contents of the Employee in the Employee form, I try to do this


List<Employee> employees = session.createCriteria(Employee.class).list();
Employee employee = employees.get(0);	
employeeForm.setItemDataSource(new BeanItem<Employee>(employee));

But this does not bind the form with the employee object. I just see empty textfields

I tried changing the EmployeeForm binding code to


setItemDataSource(new BeanItem<Employee>(employee)); 
//where employee is an object containing the Employee's details

This code displayed the content.

I just wanna display a set of Employee details one after the other using setItemDataSource.
How am i supposed to do it???

I would recommend giving the visible columns as a second parameter to setItemDataSource() to avoid internally performing some operations twice - although for most forms, this isn’t too big an issue.

I believe some information is missing here, so I’ll have to guess a little - and I don’t see much difference between what you say works and what doesn’t.

Could it be that the entity instance your criteria query returned is not fully initialized / filled in, and you run into problems with lazy loading or something like that? Are you sure the employee is fully restored? Does e.g. Hibernate.initialize(employee) before setItemDataSource() help (if using Hibernate)? Or simply calling employee.getAddress().toString()?

Actually I figured out what the issue is but couldnt find a solution yet.
There are no issues with HIbernate or fetching the Data.

I do not display the fields by directly attaching the form to the window.
Since I need a different display rather than a normal form layout, I fetch the fields from the form like this


EmployeeForm form = new EmployeeForm();
TextField firstname = (TextField) form.getField("firstName");
TextField email = (TextField) form.getField("email");
DateField dateofBirth = (DateField) form.getField("dateofBirth");
...
...
...

Once i fetch all the fields, I put them in different layouts…

Then I realized changing the datasource using setItemDataSource, changes the form’s datasource
but these fields have the initial value which is obtained from form.getField().

Is there a way I could refresh them r is there a better way to do it??

Form fields get re-created in certain situations, your field factory method might be called more than once for a field (including when changing the visible columns or setting the data source) and the instances you get from there at any given time might not be the “final” ones.

You could override Form.attachField() to place the fields in the layouts you want to use there. Make sure you handle duplicate calls for the same field.