Vaadin 如何使用BeanItemContainer按id或值选择combobox?
我使用我的组合框来满足键值对Vaadin 如何使用BeanItemContainer按id或值选择combobox?,vaadin,vaadin7,Vaadin,Vaadin7,我使用我的组合框来满足键值对 @SuppressWarnings("serial") public class ComboBoxItem implements Serializable { private String id; private String description; public ComboBoxItem(final String id, final String description) { this.id = id; this.description = d
@SuppressWarnings("serial")
public class ComboBoxItem implements Serializable {
private String id;
private String description;
public ComboBoxItem(final String id, final String description) {
this.id = id;
this.description = description;
}
public final void setId(final String id) {
this.id = id;
}
public final void setDescription(final String description) {
this.description = description;
}
public final String getId() {
return id;
}
public final String getDescription() {
return description;
}
}
我创建了一个示例组合框,如下所示
List<ComboBoxItem> lstAuctionDateList = new ArrayList<ComboBoxItem>();
lstAuctionDateList.add(new ComboBoxItem("all", "All"));
BeanItemContainer<ComboBoxItem> auctionDateItems = new BeanItemContainer<ComboBoxItem>(ComboBoxItem.class,
lstAuctionDateList);
final ComboBox cbAuctionDate = new ComboBox("Auction Date", auctionDateItems);
cbAuctionDate.addStyleName("small");
cbAuctionDate.setNullSelectionAllowed(false);
cbAuctionDate.setTextInputAllowed(false);
cbAuctionDate.setItemCaptionPropertyId("description");
cbAuctionDate.addValueChangeListener(new ValueChangeListener() {
public void valueChange(final ValueChangeEvent event) {
if (cbAuctionDate.getValue() != null) {
System.out.println(((ComboBoxItem) cbAuctionDate.getValue()).getId());
System.out.println(((ComboBoxItem) cbAuctionDate.getValue()).getDescription());
}
}
});
我怎么了?如何通过编程方式选择组合框
List lstAuctionDateList=new ArrayList();
ComboBoxItem allItems=新ComboxItem(“全部”、“全部”);
lstAuctionDateList.add(allItems);
....
...
cbAuctionDate.选择(所有项目);
当使用(bean)容器并添加项时,项本身的标识将用作容器中的itemId。例如,cbActionDate.select(lstAuctionDateList[0])
应该可以工作
您可以使对象不可变,也可以通过各种方式告诉容器它必须为id使用什么(例如,或setbeAndResolver
)
现在使对象不可变应该很容易(使类和私有属性成为最终属性,去掉setter,让IDE为您生成
equals
和hashCode
)现在我为我的问题创建了自定义组合框组件
public class ComboBox extends CustomComponent implements Serializable {
private com.vaadin.ui.ComboBox comboBox;
private BeanItemContainer<ComboBoxItem> entries = new BeanItemContainer<ComboBoxItem>(ComboBoxItem.class);
public ComboBox() {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCompositionRoot(comboBox);
}
public ComboBox(final String caption) {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCaption(caption);
setCompositionRoot(comboBox);
}
public ComboBox(final String caption, final List<ComboBoxItem> items) {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCaption(caption);
if (items != null && items.size() > 0) {
entries.addAll(items);
comboBox.setContainerDataSource(entries);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
addItems(entries);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId("description");
}
setCompositionRoot(comboBox);
}
public final void addItems(final List<ComboBoxItem> items) {
if (items != null && items.size() > 0) {
entries.addAll(items);
comboBox.setContainerDataSource(entries);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
addItems(entries);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId("description");
}
}
private void addItems(final BeanItemContainer<ComboBoxItem> items) {
comboBox.addItems(items);
}
public final void addItem(final ComboBoxItem item) {
if (item != null) {
comboBox.setContainerDataSource(entries);
comboBox.addItem(item);
comboBox.setItemCaptionPropertyId("description");
}
}
public final void selectByIndex(final int index) {
Object[] ids = comboBox.getItemIds().toArray();
comboBox.select(((ComboBoxItem) ids[index]));
}
public final void selectById(final String id) {
Object[] ids = comboBox.getItemIds().toArray();
for (int i = 0; i < ids.length; i++) {
if (((ComboBoxItem) ids[i]).getId().equals(id)) {
selectByIndex(i);
break;
}
}
}
public final void selectByItemText(final String description) {
Object[] ids = comboBox.getItemIds().toArray();
for (int i = 0; i < ids.length; i++) {
if (((ComboBoxItem) ids[i]).getDescription().equals(description)) {
selectByIndex(i);
break;
}
}
}
public final int getItemCount() {
return comboBox.getItemIds().toArray().length;
}
public final String getSelectedId() {
return ((ComboBoxItem) comboBox.getValue()).getId();
}
public final String getSelectedItemText() {
return ((ComboBoxItem) comboBox.getValue()).getDescription();
}
public final void addValueChangeListener(final ValueChangeListener listener) {
comboBox.addValueChangeListener(listener);
}
}
公共类组合框扩展了CustomComponent实现可序列化{
私有com.vaadin.ui.ComboBox组合框;
私有BeanItemContainer条目=新的BeanItemContainer(ComboBoxItem.class);
公共组合框(){
comboBox=new com.vaadin.ui.comboBox();
comboBox.addStyleName(“小”);
comboBox.SetNullSelectionLowed(false);
comboBox.setTextInputAllowed(false);
setCompositionRoot(组合框);
}
公共组合框(最终字符串标题){
comboBox=new com.vaadin.ui.comboBox();
comboBox.addStyleName(“小”);
comboBox.SetNullSelectionLowed(false);
comboBox.setTextInputAllowed(false);
设置标题(标题);
setCompositionRoot(组合框);
}
公共组合框(最终字符串标题、最终列表项){
comboBox=new com.vaadin.ui.comboBox();
comboBox.addStyleName(“小”);
comboBox.SetNullSelectionLowed(false);
comboBox.setTextInputAllowed(false);
设置标题(标题);
if(items!=null&&items.size()>0){
条目。添加所有(项目);
comboBox.setContainerDataSource(条目);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
附加项(条目);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId(“说明”);
}
setCompositionRoot(组合框);
}
公共最终作废附加项(最终列表项){
if(items!=null&&items.size()>0){
条目。添加所有(项目);
comboBox.setContainerDataSource(条目);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
附加项(条目);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId(“说明”);
}
}
专用无效附加项(最终BeanItemContainer项){
组合框。添加项(项目);
}
公共最终作废附加项(最终ComboBoxItem){
如果(项!=null){
comboBox.setContainerDataSource(条目);
comboBox.addItem(项目);
comboBox.setItemCaptionPropertyId(“说明”);
}
}
公共最终作废selectByIndex(最终整数索引){
Object[]ids=comboBox.getItemIds().toArray();
comboBox.select(((ComboBoxItem)id[index]);
}
公共最终作废selectById(最终字符串id){
Object[]ids=comboBox.getItemIds().toArray();
for(int i=0;i
下面是测试代码
final ComboBox combo = new ComboBox("My ComboBox");
combo.addItem(new ComboBoxItem("all", "All"));
// Add with list
List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
items.add(new ComboBoxItem("one", "One"));
items.add(new ComboBoxItem("two", "Two"));
items.add(new ComboBoxItem("three", "Three"));
combo.addItems(items);
combo.addItem(new ComboBoxItem("four", "Four"));
combo.addItem(new ComboBoxItem("five", "five"));
combo.selectByIndex(3);
combo.addValueChangeListener(new ValueChangeListener() {
public void valueChange(final ValueChangeEvent event) {
System.out.println(combo.getSelectedId() + " --- " + combo.getSelectedItemText());
}
});
最终组合框组合=新组合框(“我的组合框”);
combo.addItem(新ComboBoxItem(“全部”、“全部”));
//添加列表
列表项=新建ArrayList();
添加(新的ComboBoxItem(“一”、“一”));
添加(新的ComboBoxItem(“两个”、“两个”));
添加(新的ComboBoxItem(“三”、“三”));
组合附加项(项目);
combo.addItem(新ComboBoxItem(“四”、“四”));
combo.addItem(新ComboBoxItem(“五”、“五”));
组合。按索引选择(3);
combo.addValueChangeListener(新的ValueChangeListener(){
public void valueChange(最终值更改事件){
System.out.println(combo.getSelectedId()+“--”+combo.getSelectedItemText());
}
});
是!我现在就修改你的第一条建议。我同意这是不必要的。现在我的组合框是空的。我需要设置ItemCaptionPropertyId吗?现在我仍然在使用cbAuctionDate.setItemCaptionPropertyId(“description”)代码>。我还没有获取。如果您没有指定任何内容,那么toString()方法将用于显示属性cbAuctionDate.select(lstactiondatelist.get(0))代码>。。。这真是一项工作。谢谢,先生!
public class ComboBox extends CustomComponent implements Serializable {
private com.vaadin.ui.ComboBox comboBox;
private BeanItemContainer<ComboBoxItem> entries = new BeanItemContainer<ComboBoxItem>(ComboBoxItem.class);
public ComboBox() {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCompositionRoot(comboBox);
}
public ComboBox(final String caption) {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCaption(caption);
setCompositionRoot(comboBox);
}
public ComboBox(final String caption, final List<ComboBoxItem> items) {
comboBox = new com.vaadin.ui.ComboBox();
comboBox.addStyleName("small");
comboBox.setNullSelectionAllowed(false);
comboBox.setTextInputAllowed(false);
setCaption(caption);
if (items != null && items.size() > 0) {
entries.addAll(items);
comboBox.setContainerDataSource(entries);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
addItems(entries);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId("description");
}
setCompositionRoot(comboBox);
}
public final void addItems(final List<ComboBoxItem> items) {
if (items != null && items.size() > 0) {
entries.addAll(items);
comboBox.setContainerDataSource(entries);
comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
addItems(entries);
comboBox.select(items.get(0));
comboBox.setItemCaptionPropertyId("description");
}
}
private void addItems(final BeanItemContainer<ComboBoxItem> items) {
comboBox.addItems(items);
}
public final void addItem(final ComboBoxItem item) {
if (item != null) {
comboBox.setContainerDataSource(entries);
comboBox.addItem(item);
comboBox.setItemCaptionPropertyId("description");
}
}
public final void selectByIndex(final int index) {
Object[] ids = comboBox.getItemIds().toArray();
comboBox.select(((ComboBoxItem) ids[index]));
}
public final void selectById(final String id) {
Object[] ids = comboBox.getItemIds().toArray();
for (int i = 0; i < ids.length; i++) {
if (((ComboBoxItem) ids[i]).getId().equals(id)) {
selectByIndex(i);
break;
}
}
}
public final void selectByItemText(final String description) {
Object[] ids = comboBox.getItemIds().toArray();
for (int i = 0; i < ids.length; i++) {
if (((ComboBoxItem) ids[i]).getDescription().equals(description)) {
selectByIndex(i);
break;
}
}
}
public final int getItemCount() {
return comboBox.getItemIds().toArray().length;
}
public final String getSelectedId() {
return ((ComboBoxItem) comboBox.getValue()).getId();
}
public final String getSelectedItemText() {
return ((ComboBoxItem) comboBox.getValue()).getDescription();
}
public final void addValueChangeListener(final ValueChangeListener listener) {
comboBox.addValueChangeListener(listener);
}
}
final ComboBox combo = new ComboBox("My ComboBox");
combo.addItem(new ComboBoxItem("all", "All"));
// Add with list
List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
items.add(new ComboBoxItem("one", "One"));
items.add(new ComboBoxItem("two", "Two"));
items.add(new ComboBoxItem("three", "Three"));
combo.addItems(items);
combo.addItem(new ComboBoxItem("four", "Four"));
combo.addItem(new ComboBoxItem("five", "five"));
combo.selectByIndex(3);
combo.addValueChangeListener(new ValueChangeListener() {
public void valueChange(final ValueChangeEvent event) {
System.out.println(combo.getSelectedId() + " --- " + combo.getSelectedItemText());
}
});