luu*_*sen 5 java combobox vaadin
我有一个充满了containerdatasource的vaadin组合框
setContainerDataSource(container);
Run Code Online (Sandbox Code Playgroud)
我现在想在结果列表中的某处插入静态文本.
例如:
一个充满容器的Combobox,以及在结果列表中弹出的第一个条目是某种标题:
人物:
Thomas S.
Lucas B.
Alex X.
我可以通过操纵容器或组合框来实现这一目标吗?
我只是尝试设置容器源并通过addItem()向ComboBox添加一个String/Label,但这似乎不起作用.我对此有点新意,所以我不知道如何继续.
如果您立即使用ComboBox并且不希望将"Person:"作为真人处理,则可以使用setNullSelectionItemId将假人定义为真正的虚拟对象.但是,此解决方案的局限性在于您只能添加一个虚拟对象.
这是我的示例,它在列表顶部添加"Person:"并将其作为空值处理.请注意,我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)