Combobox : setItemCaptionPropertyId with only getter

Hi all,

I have a little snippet very simple, but doesn’t working.

I only want to build a combobox over an enum, and then, display custom text as itemCaption.

My code is very simple :

Combobox combo = new ComboBox("My combo");
combo.setItemCaptionPropertyId("customDisplay");
combo.addItems(Lists.newArrayList(MyEnum.values()));

[code]
public enum MyEnum {
VALUE1, VALUE2, VALUE3, VALUE4;

@Override
public String getCustomDisplay() {
    return "My " + name();
}

}
[/code]The result is only empty in the visual combox …

How can i do that please, if possible without setting manualy all item captions (aka setItemCaption(item, caption) method …) ?

Thanks

Hello,

You could try something like this:

    public enum MyEnum{
        VALUE1, VALUE2, VALUE3, VALUE4;

        public String getCustomDisplay(){
            return "My "+name();
        }

        public String toString(){
            return getCustomDisplay();
        }
    }
....

   ComboBox box=new ComboBox();
   box.addItems(MyEnum.values());

Cheers,
Matti

try:

Combobox combo = new ComboBox("My combo", new BeanItemContainer<MyEnum>(MyEnum.class));
combo.setItemCaptionPropertyId("customDisplay");
combo.addItems(Lists.newArrayList(MyEnum.values()));

Hey,

Thanks a lot !

I missed that i must use an explicit “BeanItemContainer” to force the component to index properties …

So the 2 propositions works, but i prefer to use the second one.

I hope this will help another people.

This is solved.