vaadin setItemCaptionPropertyId more then one caption in combobox

I using an BeanItemContainer to fill a combobox, like this :

//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"); The DTO has following fields :

public class SubCategoryDTO extends Observable implements Serializable {

private static final long serialVersionUID = 1L;
private int subCategoryId;
private String name;
private CategoryDTO category;

[/code]I would like to let the ItemCaption of the combobox show both the name and the category name (DTO also has a name field), so I would get something like : categoryName subCategoryName

Is there a way to do so? Any suggestion would be appreciated! Thx

I think there’s at least the following ways to accomplish that:

  • Wrap the BeanItemContainer in a GeneratedPropertyContainer, where you can generate a property that has the category names as you like.
  • Extend ComboBox and override getItemCaption() so that it returns a caption based on the two properties. See how the method is implemented in AbstractSelect; you only need to have the code for the PROPERTY caption mode. Actually, when you reimplement it in that way, setting the caption mode becomes irrelevant.

You can set the item caption on the combobox itself using ItemCaptionMode.EXPLICIT and combo.setItemCaption(id, caption) or add a read only property to your bean:

public class SubCategoryDTO {

private String name;
private CategoryDTO parent;

public String getCaption() {
return parent.getName() + " " + name;

[/code]and use ItemCaptionMode.PROPERTY and combo.setItemCaptionPropertyId(“caption”). Similarly, you can put the captioning logic inside the overridden toString() and use ItemCaptionMode.ITEM