I need to write a converter that converts first element of a list to string

Hello,

I have this attribute that is actually postal Address Line, and is returned as a List. That’s why I need a custom converter. So far I have the convertToPresentation which was more than simple, but I don’t get how to write the convertToModel? I understand that there I should take the string value from the form and put it back as my list element (in this simple case, as list item 1).

    @Override
    public Result<List<String>> convertToModel(String s, ValueContext valueContext) {
        // this one I need
    }

    @Override
    public String convertToPresentation(List<String> newtarget, ValueContext valueContext) {
        return newtarget.get(0);
    }

This is not going to work with Converter.

Instead I would add getter and setter methods to your bean for getting and setting specific address line. As you see Binder and DataProvider classes use functional interfaces to use these, which allows you to use both function references or lambdas.

Tatu Lund:
This is not going to work with Converter.

Instead I would add getter and setter methods to your bean for getting and setting specific address line. As you see Binder and DataProvider classes use functional interfaces to use these, which allows you to use both function references or lambdas.

There is a problem: I don’t have the bean. I use some Java API which works something like the following: interfaces are generated from UML models and I can use these interfaces as beans but - I can’t (or even don’t want) add anything to them or write implementation classes because these are huge.

Are you absolutely positive there is no way I could achieve writing string value from my address text field back to the first element of List<String>?

Ok, with the help from someone from Gitter.io Vaadin community the problem is now solved. I switched to using TextArea instead of TextField. It makes sense because I can (and I should) ask the users to enter the Address Lines one after another. Then, it wasn’t too complicated to write my custom converter that takes care of converting from the list of values to one single string value that contains all the lines (separated by line-break “\n”).