VaadIN 组合框 BeanContainer

VaadIN combox BeanContainer

我正在使用 beanContainer 填充组合框 en 以将 "loginName" 显示为 selection 字段。

DTO

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);

当我 select 组合框的值时,我以为我会得到 UserDTO,但我得到了登录名的字符串。有没有办法直接检索 selected UserDTO?

    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. 
        }
    });

public class SimpleBean { 私有字符串名称;

public SimpleBean(String name)
{
    this.name = name;
}


public String getName()
{
    return name;
}


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

}

据我了解,您想在调用 cbmSolver.getValue() 时获得 UserDTO-object。

这是我的建议:

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);

这将列出一个带有 object 个 UserDTO 的 ComboBox,其标题来自字段 "loginName"。调用 cbmSolver.getValue() 时,您将获得 object 而不是 "longinName".

的字符串