Java vaadin setItemCaptionPropertyId组合框中的多个标题
我使用BeanItemContainer填充组合框,如下所示:Java vaadin setItemCaptionPropertyId组合框中的多个标题,java,vaadin,Java,Vaadin,我使用BeanItemContainer填充组合框,如下所示: //filling the combobox with UserDTO's by BeanContainer BeanItemContainer<SubCategoryDTO> beanContainer = new BeanItemContainer<SubCategoryDTO>( SubCategoryDTO.class); ArrayList<SubCat
//filling the combobox with UserDTO's by BeanContainer
BeanItemContainer<SubCategoryDTO> beanContainer = new BeanItemContainer<SubCategoryDTO>(
SubCategoryDTO.class);
ArrayList<SubCategoryDTO> subcategorys = qpc.getSubcategorys();
beanContainer.addAll(subcategorys);
cmbCategory.setContainerDataSource(beanContainer);
cmbCategory.setItemCaptionMode(ItemCaptionMode.ID);
cmbCategory.setImmediate(true);
cmbCategory.setNewItemsAllowed(false);
cmbCategory.setNullSelectionAllowed(false);
cmbCategory.setItemCaptionPropertyId("name");
我想让组合框的ItemCaption同时显示名称和类别名称(DTO也有一个名称字段),因此我会得到如下内容:categoryName subCategoryName
有没有办法做到这一点?任何建议都将不胜感激!Thx有一种方法(可能有更简单的方法)。您可以迭代组合框中的项目并设置项目标题
像这样:
for(Object subCatDTO : cbmCategory.getItemIds()){
SubCategoryDTO subCategoryDTO = (SubCategoryDTO) subCatDTO;
cbmCategory.setItemCatption(subCatDTO, subCategoryDTO.getName + " " + subCategoryDTO.getCategory().getName());
}
我想这可以解决你的问题。
有关详细信息:您可以使用
ItemCaptionMode.EXPLICIT
和combo.setItemCaption(id,caption)
在组合框本身上设置项目标题,或者向bean添加只读属性:
public class SubCategoryDTO {
private String name;
private CategoryDTO parent;
public String getCaption() {
return parent.getName() + " " + name;
}
}
并使用
ItemCaptionMode.PROPERTY
和combo.setItemCaptionPropertyId(“caption”)
。类似地,您可以将字幕逻辑放在重写的toString()
中,并使用ItemCaptionMode.ITEM
combo.setItemCaptionPropertyId(“caption”)实现了这一技巧。谢谢您能否提供一个示例,说明如何覆盖toString()
,以及覆盖哪一个和在何处?我希望将此技术与ItemCaptionMode.ITEM
结合使用,但我不清楚该如何操作。
public class SubCategoryDTO {
private String name;
private CategoryDTO parent;
public String getCaption() {
return parent.getName() + " " + name;
}
}