Java 向带有数据源的组合框中添加文本
我有一个vaadin组合框,里面装满了containerdatasourceJava 向带有数据源的组合框中添加文本,java,combobox,vaadin,Java,Combobox,Vaadin,我有一个vaadin组合框,里面装满了containerdatasource setContainerDataSource(container); 现在我想在结果列表的某个地方插入一个静态文本 例如: 一个组合框中填充了的容器,结果列表中弹出的第一个条目是某种类型的标题: 人员: 托马斯S. 卢卡斯B. 亚历克斯X 我可以通过操纵容器或组合框来实现这一点吗 我只是尝试设置容器源并通过addItem()向组合框添加字符串/标签,但这似乎不起作用。我对此有点陌生,所以我不知道如何继续。您应该在容
setContainerDataSource(container);
现在我想在结果列表的某个地方插入一个静态文本
例如: 一个组合框中填充了的容器,结果列表中弹出的第一个条目是某种类型的标题: 人员:
托马斯S.
卢卡斯B.
亚历克斯X 我可以通过操纵容器或组合框来实现这一点吗
我只是尝试设置容器源并通过addItem()向组合框添加字符串/标签,但这似乎不起作用。我对此有点陌生,所以我不知道如何继续。您应该在容器中进行更改(例如:添加项…)并在组合框上再次调用setContainerDataSource(容器)(以便将其传播到客户端)。如果您的代码与此类似:
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
BeanItemContainer=新的BeanItemContainer(Person.class);
container.addAll(myPersonList);
ComboBox ComboBox=新建ComboBox();
combobox.setContainerDataSource(容器);
combobox.setItemCaptionMode(选择.ITEM\u CAPTION\u MODE\u属性);
combobox.setItemCaptionPropertyId(“名称”);//该人员的姓名字段将显示在UI上
//imho如果要将静态文本(字符串)添加到容器中
//其中填充了Person对象,然后您必须创建一个伪Person对象
Person staticText=新的Person();
setName(“我的静态文本”);
combobox.addItem(静态文本);
//如果要指定项的索引,请在for cycle中逐个添加它们
//并在适当的位置插入静态文本项
如果您使用组合框作为即时对象,并且不希望将“Person:”作为真人处理,则可以使用来将假人定义为真实的虚拟对象。但是,此解决方案有一个限制,即只能添加一个虚拟对象
下面是我的示例,它在列表顶部添加“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);
}
}
import com.vaadin.data.Property;
导入com.vaadin.data.Property.ValueChangeEvent;
导入com.vaadin.data.util.BeanItemContainer;
导入com.vaadin.server.vaadin请求;
导入com.vaadin.ui.AbstractSelect;
导入com.vaadin.ui.ComboBox;
导入com.vaadin.ui.Notification;
导入com.vaadin.ui.ui;
导入com.vaadin.ui.VerticalLayout;
/**
*应用程序的“主”类
*/
@抑制警告(“串行”)
公共类MyVaadinUI扩展UI{
@凌驾
受保护的void init(VaadinRequest请求){
最终垂直布局=新建垂直布局();
布局。设置页边距(真);
设置内容(布局);
BeanItemContainer=新的BeanItemContainer(Person.class);
Person nullPerson=新的Person(0,“Person:”);
addBean(nullPerson);
container.addBean(新人(1,“Django”);
container.addBean(新人(2,“舒尔茨”);
ComboBox ComboBox=新建ComboBox();
combobox.setImmediate(true);
combobox.setNullSelectionItemId(nullPerson);//将nullPerson定义为一个伪对象。
combobox.setContainerDataSource(容器);
combobox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
combobox.setItemCaptionPropertyId(“名称”);//用户界面上将显示该人员的姓名字段
combobox.addValueChangeListener(新属性.ValueChangeListener(){
@凌驾
public void valueChange(valuechangevent事件){
//选择nullPerson时将显示“null selected”。
Notification.show(event.getProperty().getValue()+“选定”);
}
});
布局。添加组件(组合框);
}
}
如果您发布您迄今为止尝试或研究的内容,可能会有所帮助。我只是尝试设置容器源,并通过addItem()向组合框添加字符串/标签,但这似乎不起作用。我对这个有点陌生,所以我不知道如何继续。试过了,我有一个装满人的容器,试过了容器;“test”将不会显示在列表中,但容器的其余部分会显示。很好,但我想知道如何获得该人员的id