How do I select a combobox by id or value using a BeanItemContainer?

I am using BeanItemContainer for my mappings to satisfy key-value pairs.

@SuppressWarnings("serial")
public class ComboBoxItem implements Serializable {
private String id;
private String description;

public ComboBoxItem(final String id, final String description) {
    this.id = id;
    this.description = description;
}

public final void setId(final String id) {
    this.id = id;
}

public final void setDescription(final String description) {
    this.description = description;
}

public final String getId() {
    return id;
}

public final String getDescription() {
    return description;
}

      

}

I have created a sample combobox as shown below

    List<ComboBoxItem> lstAuctionDateList = new ArrayList<ComboBoxItem>();
    lstAuctionDateList.add(new ComboBoxItem("all", "All"));
    BeanItemContainer<ComboBoxItem> auctionDateItems = new BeanItemContainer<ComboBoxItem>(ComboBoxItem.class,
            lstAuctionDateList);
    final ComboBox cbAuctionDate = new ComboBox("Auction Date", auctionDateItems);
    cbAuctionDate.addStyleName("small");
    cbAuctionDate.setNullSelectionAllowed(false);
    cbAuctionDate.setTextInputAllowed(false);
    cbAuctionDate.setItemCaptionPropertyId("description");
    cbAuctionDate.addValueChangeListener(new ValueChangeListener() {

        public void valueChange(final ValueChangeEvent event) {
            if (cbAuctionDate.getValue() != null) {
                System.out.println(((ComboBoxItem) cbAuctionDate.getValue()).getId());
                System.out.println(((ComboBoxItem) cbAuctionDate.getValue()).getDescription());
            }

        }
    });

      

This is fine, but I am unable to select any of the combobox items using the codes below.

    cbAuctionDate.select("all");
    cbAuctionDate.select("All");
    cbAuctionDate.setValue("all");
    cbAuctionDate.setValue("All");

      

What am I wrong? How can I choose my combinations programmatically?

+3


source to share


3 answers


when using a container (bean) and adding items, the very item ID itself is used as the itemId in the container. For example. cbActionDate.select(lstAuctionDateList[0])

must work.

You either made your objects immutable, or you used ways to tell the container what it should use for the identifier (for example, setBeanIdProperty("id")

or setBeanIdResolver

).



Make the object the same should be easy right now (do class and private attributes final, drop setters and allow your environment to generate ID equals

and hashCode

for you)

+4


source


  • You don't need to call cbAuctionDate.addItem ("All"), you already have such an item in your collection

  • I would try like this:



List<ComboBoxItem> lstAuctionDateList = new ArrayList<ComboBoxItem>();
ComboBoxItem allItems= new ComboBoxItem("all", "All");
lstAuctionDateList.add(allItems);
....
...
cbAuctionDate.select(allItems);

      

+3


source


Now I have created a custom ComboBox component for my problem

public class ComboBox extends CustomComponent implements Serializable {
private com.vaadin.ui.ComboBox comboBox;
private BeanItemContainer<ComboBoxItem> entries = new BeanItemContainer<ComboBoxItem>(ComboBoxItem.class);

public ComboBox() {
    comboBox = new com.vaadin.ui.ComboBox();
    comboBox.addStyleName("small");
    comboBox.setNullSelectionAllowed(false);
    comboBox.setTextInputAllowed(false);
    setCompositionRoot(comboBox);
}
public ComboBox(final String caption) {
    comboBox = new com.vaadin.ui.ComboBox();
    comboBox.addStyleName("small");
    comboBox.setNullSelectionAllowed(false);
    comboBox.setTextInputAllowed(false);
    setCaption(caption);
    setCompositionRoot(comboBox);
}

public ComboBox(final String caption, final List<ComboBoxItem> items) {
    comboBox = new com.vaadin.ui.ComboBox();
    comboBox.addStyleName("small");
    comboBox.setNullSelectionAllowed(false);
    comboBox.setTextInputAllowed(false);
    setCaption(caption);
    if (items != null && items.size() > 0) {
        entries.addAll(items);
        comboBox.setContainerDataSource(entries);
        comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
        addItems(entries);
        comboBox.select(items.get(0));
        comboBox.setItemCaptionPropertyId("description");
    }

    setCompositionRoot(comboBox);
}
public final void addItems(final List<ComboBoxItem> items) {
    if (items != null && items.size() > 0) {
        entries.addAll(items);
        comboBox.setContainerDataSource(entries);
        comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
        addItems(entries);
        comboBox.select(items.get(0));
        comboBox.setItemCaptionPropertyId("description");
    }
}
private void addItems(final BeanItemContainer<ComboBoxItem> items) {
    comboBox.addItems(items);
}

public final void addItem(final ComboBoxItem item) {
    if (item != null) {
        comboBox.setContainerDataSource(entries);
        comboBox.addItem(item);
        comboBox.setItemCaptionPropertyId("description");
    }
}

public final void selectByIndex(final int index) {
    Object[] ids = comboBox.getItemIds().toArray();
    comboBox.select(((ComboBoxItem) ids[index]));
}

public final void selectById(final String id) {
    Object[] ids = comboBox.getItemIds().toArray();
    for (int i = 0; i < ids.length; i++) {
        if (((ComboBoxItem) ids[i]).getId().equals(id)) {
            selectByIndex(i);
            break;
        }
    }
}

public final void selectByItemText(final String description) {
    Object[] ids = comboBox.getItemIds().toArray();
    for (int i = 0; i < ids.length; i++) {
        if (((ComboBoxItem) ids[i]).getDescription().equals(description)) {
            selectByIndex(i);
            break;
        }
    }
}

public final int getItemCount() {
    return comboBox.getItemIds().toArray().length;
}

public final String getSelectedId() {
    return ((ComboBoxItem) comboBox.getValue()).getId();
}
public final String getSelectedItemText() {
    return ((ComboBoxItem) comboBox.getValue()).getDescription();
}

public final void addValueChangeListener(final ValueChangeListener listener) {
    comboBox.addValueChangeListener(listener);
}
}

      

and below are test codes

    final ComboBox combo = new ComboBox("My ComboBox");
    combo.addItem(new ComboBoxItem("all", "All"));
    // Add with list
    List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
    items.add(new ComboBoxItem("one", "One"));
    items.add(new ComboBoxItem("two", "Two"));
    items.add(new ComboBoxItem("three", "Three"));
    combo.addItems(items);

    combo.addItem(new ComboBoxItem("four", "Four"));
    combo.addItem(new ComboBoxItem("five", "five"));
    combo.selectByIndex(3);
    combo.addValueChangeListener(new ValueChangeListener() {

        public void valueChange(final ValueChangeEvent event) {
            System.out.println(combo.getSelectedId() + " --- " + combo.getSelectedItemText());

        }
    });

      

+1


source







All Articles