我使用一个beanContainer来填充一个组合框en,以显示"loginName“作为选择字段。
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);
当我选择组合框的值时,我想我会得到UserDTO,但是我得到了loginName的字符串。是否有一种直接检索所选UserDTO的方法?
发布于 2015-05-10 19:46:46
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 {私有字符串名称;
public SimpleBean(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
发布于 2015-05-11 11:04:45
据我所知,在调用cbmSolver.getValue()时,您希望获得UserDTO对象。
以下是我的建议:
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“的字符串。
https://stackoverflow.com/questions/30155011
复制相似问题