vaadin组合框项目标签和项目值

sog*_*ukk 1 java vaadin

我与JSF background.Currently新Vaadin用户我试图用vaadin.What我想要做的是实现一个简单的组合框,使用List作为组合框的ITEMLIST,显示在组合框中Example.description领域,当的项目的一个选择的获取对象实例/ example.id作为值(如我们在JSF做使用itemLabel = example.description,项目值=实施例/ F的example.id属性:selectItems的).

小智 10

这是来自Vaadin团队的Ville.您可以通过很多方式执行此操作,但通常可以使用setItemCaptionMode()方法切换ComboBox行为.

但是,通过以下示例完成了与您尝试执行的操作非常接近的操作:

public class Example {

    private Integer id;
    private String description;

    public Example(Integer id, String description) {
        this.id = id;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

@Override
public void init() {
    Window mainWindow = new Window("Combobox Application");

    List<Example> examples = new ArrayList<Example>();
    examples.add(new Example(new Integer(1), "First description"));
    examples.add(new Example(new Integer(2), "Second description"));
    examples.add(new Example(new Integer(3), "Third description"));
    BeanItemContainer<Example> objects = new BeanItemContainer(Example.class, examples);

    ComboBox combo = new ComboBox("Example", objects);
    combo.setItemCaptionPropertyId("description");

    mainWindow.addComponent(combo);
    setMainWindow(mainWindow);
}
Run Code Online (Sandbox Code Playgroud)

这里BeanItemContainer包装你的POJO并使用反射来访问getter.

干杯.