Bind is not working in NativeSelect.

Hi DataBinding is not wroking even add Convertor.



How can we added Native Select , Convertor is not called at all .


I have BeanItemContainer to load Native select.

public class Department implements Serializable{
private static final long serialVersionUID = 1L;
private Long deptId;
private String name;
// setters & getters.
}

public class Employee implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;

private String firstName;
private String lastName;
private Long department;

// working fine if departmet is Department type, but my requirement is only Long value will come in field bean.
}

Field Group with bind;
Employee emp=new Employee();
emp.setDepartment(new Long(1));
emp.setFirstName(“Palanivel”);
emp.setLastName(“Kainattu”);

fieldGroup = new BeanFieldGroup(Employee.class);
fieldGroup.setItemDataSource(emp);
fieldGroup.bindMemberFields(this);

and Convertor is .

public class SelectConvertor implements Converter< Department,Long>{

private static final long serialVersionUID = -220989690852205509L;

@Override
public Long convertToModel(Department value,
Class<? extends Long> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
System.out.println(“–> getting”);

return (value!=null?value.getDeptId():null);
}

@Override
public Department convertToPresentation(Long value,
Class<? extends Department> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
Department dept = new Department();
dept.setDeptId(value);
System.out.println(“–> getting”);
return dept;
}

@Override
public Class getModelType() {
System.out.println(“–> getting”);
return Long.class;
}

@Override
public Class getPresentationType() {
System.out.println(“–> getting”);
return Department.class;
}
}

I’m fighting with this too. I’m having very similar usage. The bind sets property datasource, but this does not set the value of the combo so it appears empty, however I can select values form the list and they stay visible until combo gets focus lost event. Them the combo clears, but change events are fired, so the bean inside beanItem gets changed.
Do you have any progress there ?

Hi Pavel,

I Found the solution .

you have to do following steps .

The Converter will not call for Native select by defautl .( from the documantation ).

Step 1: create Converter. Hope you know how to do.
// i will help you , if stillneed help. :slight_smile:
Step 2 : You have to crate your own Converter Factory class and you have to register.

public class StringToObjectConverterFactory extends DefaultConverterFactory {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;

@SuppressWarnings("unchecked")
@Override
public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
    
    //TODO to be removed. Handle one particular type conversion
    
    Logger.debug("-->  presentationType:"+presentationType);
    Logger.debug("--> modelType "+ modelType);
    
    if (Object.class == presentationType && Department.class == modelType)
        return (Converter<PRESENTATION, MODEL>) new SelectConvertor();
    // Default to the supertype
    return super.createConverter(presentationType, modelType);
}

}
Step 3 : Register Converter Factory into UI class.

VaadinSession.getCurrent().setConverterFactory( new StringToObjectConverterFactory());

that’s it .

Regards
Palanivel.