In Vaadin 7, a converter is associated with a field. In Vaadin 8, it is associated with a binding.
If a bean has an integer property with name intField that is bound to a text field I need to use a converter like this:
Binder binder = new Binder( bean.getClass() );
HasValue field = new TextField("intField");
binder.forField( field ).withConverter( new StringToIntegerConverter( "Enter a number" ) )
.bind( "intField" );
binder.setBean( bean );
In Vaadin 7, in order to populate the field directly, I could say
field.setValue( new Integer(1) );
But now, in Vaadin 8, it throws an exception
java.lang.Integer cannot be cast to java.lang.String
Is there a way in Vaadin 8 to populate a field directly? Do I have to populate a property and then read the entire bean? The problem with that approach is that if I populate a field then correspdning property of the bean will be populated automatically. If I populate a property then I have to also call the readBean method of the binder to also populate the field. It does not happen automatically like everything else.
Thank you. I should have been more specific in the original post. Let me show more code to illustrate why I asked the question. In Vaadin 7, we have done this[code]
public class NumberTextField extends TextField {
NumberExtension numberExtension;
private void init( FieldType fieldType, boolean isCurrency, boolean isPercent, int precision ) {
numberExtension = new NumberExtension();
…
@Override
public void validate( String value ) throws com.vaadin.data.Property.ReadOnlyException {
…
[/code]Then, because the converter is associated with the field itself, you could populate the numeric field with a numeric value. The associated converter would convert the value to what the field consumes.
AbstractField field = new NumberTextField( fieldDefinition.getFieldType(), fieldDefinition.getName(), false, false, Integer.MAX_VALUE );
field.setValue( new Double(1.0) );
It seems in Vaadin 8 there is no easy way to populate the field directly like that without using the binder/bean because the field does not have a converter, only binder does.
Obviously, the binder is aware of the field. Why isn’t the field aware of the binder? It’s not like a field can be bound to more than once. If the field knows its binder then it can access the converter, just like before in Vaadin 7.