首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VaadIN combox BeanContainer

VaadIN combox BeanContainer
EN

Stack Overflow用户
提问于 2015-05-10 18:20:19
回答 2查看 260关注 0票数 0

我使用一个beanContainer来填充一个组合框en,以显示"loginName“作为选择字段。

DTO

代码语言:javascript
运行
复制
public class UserDTO extends Observable implements Serializable {

private static final long serialVersionUID = 1L;
private int userId;
private String loginName;
private String password;
private String firstName;
private String lastName;
private AddressDTO address;
private boolean privacyAddress;
private String pictureLocation;
private Date birthDate;
private RightDTO right;
private String email;
private String telephone;
private boolean privacyTelephone;
private String mobile;
private boolean privacyMobile;
private int minLimit;
private int maxLimit;
private int balance;
private boolean active;
...

//filling the combobox with UserDTO's by BeanContainer
    BeanContainer<String, UserDTO> beanContainer = new BeanContainer<String, UserDTO>(
            UserDTO.class);
    beanContainer.setBeanIdProperty("loginName");
    cmbSolver.setImmediate(true);
    cmbSolver.setNewItemsAllowed(false);
    cmbSolver.setNullSelectionAllowed(false);
    cmbSolver.setContainerDataSource(beanContainer);
    cmbSolver.setItemCaptionMode(ItemCaptionMode.ID);
    cmbSolver.setItemCaptionPropertyId("loginName");
    ArrayList<UserDTO> solvers = pc.retrieveSolvers();
    for (int i = 0; i < solvers.size(); i++) {
        System.out.println("solver = " + solvers.get(i));
    }
    beanContainer.addAll(solvers);

当我选择组合框的值时,我想我会得到UserDTO,但是我得到了loginName的字符串。是否有一种直接检索所选UserDTO的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-10 19:46:46

代码语言:javascript
运行
复制
    final BeanItemContainer<SimpleBean> container = new BeanItemContainer<>(SimpleBean.class);//create a container for beans
    container.addBean(new SimpleBean("Some string"));//add bean to the container
    container.addBean(new SimpleBean("Some string 123"));
    final ComboBox combo = new ComboBox("choose", container);/*create a combo box with caption "choose". the second argument is the datasource - this is the place from where the combo box will get its values.*/
    combo.setItemCaptionMode(ItemCaptionMode.ID);/*this will "tell" to combo box to use the container ids as values that must be showed in the combo box*/
    combo.setItemCaptionPropertyId("name");//"tell" to the combobox from which property of the container to get the values which must be displayed to the user
    combo.setImmediate(true);//this will generate browser request to the server immediate after user change the value of the combobox
    combo.addValueChangeListener(new ValueChangeListener()
    {

        @Override
        public void valueChange(ValueChangeEvent event)/*this method will be invoked when the combo box value was changed*/
        {

            System.out.println(combo.getValue());//this will return the selected object. 
        }
    });

公共类SimpleBean {私有字符串名称;

代码语言:javascript
运行
复制
public SimpleBean(String name)
{
    this.name = name;
}


public String getName()
{
    return name;
}


public void setName(String name)
{
    this.name = name;
}

}

票数 1
EN

Stack Overflow用户

发布于 2015-05-11 11:04:45

据我所知,在调用cbmSolver.getValue()时,您希望获得UserDTO对象。

以下是我的建议:

代码语言:javascript
运行
复制
BeanItemContainer<UserDTO> beanContainer = new BeanItemContainer<UserDTO>(UserDTO.class);
    beanContainer.setBeanIdProperty("loginName");
    cmbSolver.setImmediate(true);
    cmbSolver.setNewItemsAllowed(false);
    cmbSolver.setNullSelectionAllowed(false);
    cmbSolver.setContainerDataSource(beanContainer);
    cmbSolver.setItemCaptionMode(ItemCaptionMode.ID);
    cmbSolver.setItemCaptionPropertyId("loginName");
    ArrayList<UserDTO> solvers = pc.retrieveSolvers();
    beanContainer.addAll(solvers);

这将列出一个带有来自“ComboBox”字段的标题的UserDTO对象的loginName。当调用cbmSolver.getValue()时,您将得到对象,而不是"longinName“的字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30155011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档