java 将文本添加到带有数据源的组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14895678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:45:07  来源:igfitidea点击:

adding text to a combobox with a datasource

javacomboboxvaadin

提问by luuksen

I have a vaadin combobox that is filled with a containerdatasource

我有一个充满容器数据源的 vaadin 组合框

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:

一个 Combobox 填充了一个容器,结果列表中弹出的第一个条目是某种标题:

Persons:
Thomas S.
Lucas B.
Alex X.

人员:
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.

我只是尝试设置容器源并通过 addItem() 向 ComboBox 添加字符串/标签,但这似乎不起作用。我对这个有点陌生,所以我不知道如何继续。

回答by miq

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

如果您直接使用 ComboBox 并且不希望将“人:”作为真人处理,您可以使用setNullSelectionItemId将假人定义为真正的虚拟对象。但是,此解决方案有一个限制,即您只能添加一个虚拟对象。

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.

这是我的示例,它在列表顶部添加“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);
    }
}

回答by kszk

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

回答by Ondrej Kvasnovsky

You should do the change in the container (for example: add items...) and call setContainerDataSource(container) again on the combobox (so it gets propagated to the client).

您应该在容器中进行更改(例如:添加项目...)并在组合框上再次调用 setContainerDataSource(container) (因此它会传播到客户端)。