Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java vaadin setItemCaptionPropertyId组合框中的多个标题_Java_Vaadin - Fatal编程技术网

Java vaadin setItemCaptionPropertyId组合框中的多个标题

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

我使用BeanItemContainer填充组合框,如下所示:

//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;
  }

}