Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding text to a combobox with a datasource

I have a vaadin combobox that is filled with a containerdatasource

setContainerDataSource(container);

I now want to insert a static text somewhere in the list of results.


For example:

A Combobox that is filled with a container of and the first entry that pops up in the result list is some kind of header:

Persons:
Thomas S.
Lucas B.
Alex X.

Can i achieve that by either manipulating the container or the combobox?

I just tried to set the container source and add a String/Label via addItem() to the ComboBox, but that doesn't seem to work. I am kinda new to this, so I don't know how to continue.

like image 502
luuksen Avatar asked Feb 15 '13 13:02

luuksen


People also ask

How do I add text to a ComboBox in VB?

To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.


2 Answers

If you are using the ComboBox as immediate and don't want the "Person:" to be handled as a real person, you could use setNullSelectionItemId to define the fake person as a true dummy object. This solution, however, has the limitation that you can only add one dummy object.

Here's my example which adds "Person:" on top of the list and handles it as a null value. Note that I'm using Vaadin 7.

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class);
        Person nullPerson = new Person(0, "Person:");
        container.addBean(nullPerson);
        container.addBean(new Person(1, "Django"));
        container.addBean(new Person(2, "Schultz"));

        ComboBox combobox = new ComboBox();
        combobox.setImmediate(true);
        combobox.setNullSelectionItemId(nullPerson); // Define the null person as a dummy.
        combobox.setContainerDataSource(container);
        combobox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
        combobox.setItemCaptionPropertyId("name");  // the person's name field will be shown on the UI
        combobox.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                // Will display 'null selected' when nullPerson is selected.
                Notification.show(event.getProperty().getValue() + " selected");
            }
        });

        layout.addComponent(combobox);
    }
}
like image 72
miq Avatar answered Nov 14 '22 21:11

miq


If your code is similar to this:

BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class);
container.addAll(myPersonList);
ComboBox combobox = new ComboBox();
combobox.setContainerDataSource(container);
combobox.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
combobox.setItemCaptionPropertyId("name");  // the person's name field will be shown on the UI

// imho if you want to add a static text (String) into a container
// which populated with Person objects then you have to make a fake Person object
Person staticText = new Person();
staticText.setName("My static text");
combobox.addItem(staticText);
// if you want to specify the index of the item, add them one by one in for cycle
// and insert the static text item in the appropritate place
like image 20
kszk Avatar answered Nov 14 '22 22:11

kszk