Binding different components with different to-string converters to the sam

Hi,

I have this grid with a non-dynamic buffered editor.
The object i am trying to bind has two string values.
I would like to be able to choose the way i edit the second one according to the first one.
I choose the first one with a ComboBox.
For example, if the first one is ‘String’, it’s easy to bind the second because i just have to bind the second property to a TextField.
But if the first is ‘Integer’ i’d like to choose the second one with a NumberField with a Converter from Double to Integer to String.
If the first is ‘Float’ i’d like to choose the second one with a NumberField with a Converter from Double to String.
If the first is ‘LocalDateTime’ i’d like to choose the second one with a DateTimePicker with a Converter from LocalDateTime to String.
etc…
It’s also important that the second one stays one String value that may contains a String, an Integer, etc, because i don’t want my bean to have more properties.

So i would like to know if it’s possible to bind different components with different to-string converters to the same bean property in a grid editor?

Why don’t you want your bean to have more properties?

I was asked to create a class we could use in different situations so the count of needed properties would be different each time, and i wanted all theses values to be in only the second column of my grid (that is why they’re all String), but i did not find any to-string converters.

I don’t know your context, but generally speaking, I’d say it’s not a good idea to store typed data in a String property. I’d recommend rethinking the data model, if at all possible.

Anyway, if you absolutely need to, I was thinking that you could possibly create a custom String-typed Field (extend AbstractField or implement HasValue) which would handle the presentation with the appropriate Component. Let’s call it MultiComponentEditorField for the sake of the example. MultiEditorComponentField should be able to toggle the Component when the first editor component’s value changes (just a normal ValueChangeListener on the first editor). Then you would be able to do the model value generation internally in MultiComponentEditorField’s setModelValue - based on which Component is shown there. This would mean that you wouldn’t need to use a Converter at all - MultiComponentEditorField would have String as its presentation and model type, so you could bind it directly to your bean’s String property.

There’s some documentation that may help here: https://vaadin.com/docs/v14/flow/binding-data/tutorial-flow-field.html

Thank you for your answer and the doc!
I am working with a class where a ResultSet is set in a grid as vector so everything is String, and, in the grid I am making, the first property is the name of the column and the second one the value.
If the MultiComponentEditorField extends AbtractField<TextField,String>, it would have the needed components as parameter and toggle with the setVisible method? I am not very used to custom component so I don’t know if that is what you meant.

Instead of using a ResultSet directly, you could create an interim class (a DTO) instead. That’s a [pretty common pattern]
(https://en.wikipedia.org/wiki/Data_transfer_object) in GUI applications.

If the MultiComponentEditorField extends AbtractField<TextField,String>, it would have the needed components as parameter and toggle with the setVisible method? I am not very used to custom component so I don’t know if that is what you meant.

Sounds like that is one way it could work, although it should probably be extends AbstractField<MultiComponentEditorField, String>.

Thank you for your help and your advice!