java 组合框 输入数据_java – 使用数据源将文本添加到组合框中

如果您使用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 container = new BeanItemContainer(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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值