如何使用beanitemcontainer设置vaadin组合框值?

时间:2017-02-09 11:58:05

标签: java combobox javabeans vaadin7

我正在尝试设置组合框值来更新vaadin中的bean项目。但是当我打开它时,组合框选择的值为null。我怎样才能绑定价值?

    BeanItemContainer<Company> itemContainer = new BeanItemContainer<Company>(Company.class);
    itemContainer.addAll(companyService.getAllCompanies());

    cbxCompanyName = new ComboBox("Company Name", itemContainer);
    cbxCompanyName.setWidth("50%");
    cbxCompanyName.setNullSelectionAllowed(false);
    cbxCompanyName.setItemCaptionMode(ItemCaptionMode.PROPERTY);
    cbxCompanyName.setImmediate(true);
    cbxCompanyName.setItemCaptionPropertyId("companyName");
    cbxCompanyName.setNewItemsAllowed(false);
    details.addComponent(cbxCompanyName);

    cbxCompanyName.setValue(admin.getCompany());

enter image description here

2 个答案:

答案 0 :(得分:2)

enter image description here

以下是代码:

@Theme("mytheme")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        layout.setSpacing(true);
        setContent(layout);

        //cache the beans
        ArrayList<MyBean> beans = getBeans();

        BeanItemContainer container = new BeanItemContainer<>(MyBean.class, beans);

        ComboBox combo = new ComboBox("My Combo");              
        combo.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
        combo.setItemCaptionPropertyId("field");
        combo.setContainerDataSource(container);

        //select programmatically
        combo.select(beans.get(1));//this is the key idea! Provide the same bean from cache, for selection.

        layout.addComponent(combo);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }

    public class MyBean {

        private final int id;
        private final String field;

        public MyBean(int id, String field) {
            this.id = id;
            this.field = field;
        }

        public int getId() {
            return id;
        }

        public String getField() {
            return field;
        }

    }

    public ArrayList<MyBean> getBeans() {
        ArrayList<MyBean> beans = new ArrayList<>();

        MyBean bean = new MyBean(1, "Vikrant");
        beans.add(bean);

        bean = new MyBean(2, "John");
        beans.add(bean);

        bean = new MyBean(3, "Rahul");
        beans.add(bean);

        return beans;
    }

答案 1 :(得分:0)

这是我的解决方案;

    cbxCompany.select(cbxCompany.getItemIds().toArray()[getCompanyIndex()]);
    /**
     *
     *
     **/
    for (int i = 0; i < cbxCompany.getItemIds().toArray().length; i++) {
                if (((Company) cbxCompany.getItemIds().toArray()[i]).getCompanyID() == admin.getCompany().getCompanyID()) {
                    return i;
                }
            }