DarthBlueRay
DarthBlueRay

Reputation: 5

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

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

Upvotes: 0

Views: 2885

Answers (2)

Raffaele
Raffaele

Reputation: 20885

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

}

and use ItemCaptionMode.PROPERTY and combo.setItemCaptionPropertyId("caption"). Similarly, you can put the captioning logic inside the overridden toString() and use ItemCaptionMode.ITEM

Upvotes: 2

Mikkel
Mikkel

Reputation: 1

There is a way (there may be easier ways). You can iterate over the items in the ComboBox and set an item caption.

Like this:

for(Object subCatDTO : cbmCategory.getItemIds()){
    SubCategoryDTO subCategoryDTO = (SubCategoryDTO) subCatDTO;
    cbmCategory.setItemCatption(subCatDTO, subCategoryDTO.getName + " " + subCategoryDTO.getCategory().getName());
}

I think that may solve your problem. For more information: https://vaadin.com/forum/#!/thread/1394968

Upvotes: 0

Related Questions