Nicklas2
(Nicklas Karlsson)
December 18, 2015, 11:16am
1
A bit of a noob question but I’m a bit stuck with converters and the ComboBox.
I have an entity like
public class Customer
{
… private String customerCode;
}
which I would like to bind to a String attribute of another bean, what would be the way to wire this up so that I could
a) use the complete Customer object in my ComboBox (it’s handy to have in the ValueChangeListener and the caption is easily derived from the instance)
b) bind just the customerCode to the String in my target instance (so that the initial Customer would also be selected by the String-binding)?
I read the reply by Roger Krüger on SO but It appeared to be more about the problem and less about a solution so now I’m confused on a higher level.
Thanks in advance,
Nik
(hoping that Christmas comes early this year and someone answers)
Sergey137
(Sergey Budkin)
December 21, 2015, 12:20pm
2
Hi,
not sure whether I understood the problem in all entirety, but the usual approach with ComboBox is as follows:
combobox.addItem(<your Object>);
combobox.setItemCaption(<your Object>, <caption>);
That way you can fill ComboBox with any type of items, set the captions freely, and bind the combobox to the field which will be set to selected .
Merry Christmas!
Sergey.
Nicklas2
(Nicklas Karlsson)
December 21, 2015, 1:26pm
3
Hi,
not sure whether I understood the problem in all entirety, but the usual approach with ComboBox is as follows:
combobox.addItem(<your Object>);
combobox.setItemCaption(<your Object>, <caption>);
That way you can fill ComboBox with any type of items, set the captions freely, and bind the combobox to the field which will be set to selected .
Merry Christmas!
Sergey.
Thanks for the reply but the problem is not the display of the value, it’s using just a property of the bean as the actual value…
ASimple
(ASimple Dev)
December 23, 2015, 1:05pm
4
I think you have to rewrite your own converter like this
public class PConverter implements Converter<String, BeanP> {
@Override
public BeanP convertToModel(String value, Class<? extends BeanP> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
for(BeanP p : EJBUtil.getListeBeanP().getItemIds()){
if(p.getNom().equals(value)){
return p;
}
}
return null;
}
@Override
public String convertToPresentation(BeanP value, Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return value != null? value.getNom() : null; // Here select what you want to show as displayed value
}
@Override
public Class<BeanP> getModelType() {
return BeanP.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}
And set it as defaut converter for your combobox
ComboBox combo = new ComboBox();
combo.setConverter((Converter) new PConverter());