Java Vaadin 在填充后设置组合框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19301728/
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-08-12 15:50:42  来源:igfitidea点击:

Vaadin set combo box value after it has been filled

javacomboboxvaadinvaadin7

提问by Br0thazS0ul

My instance of the combo box is created globally, It gets filled with, lets say a list of companies while the value is an ID. After I load a File, I want to check that I have that value in the combo box and then select it programmatically.

我的组合框实例是全局创建的,它被填充了,比如一个公司列表,而值是一个 ID。加载文件后,我想检查组合框中是否具有该值,然后以编程方式选择它。

class cComboBoxFun extends UI implements ClickListener
{
  ComboBox cb_company;
  List<cCustomer> ListCust;

  //default constructor and server conf not really relevant

  @Override
protected void init(VaadinRequest request) 
{
      //Lets assume the list has been filled already
      cb_company = new ComboBox("Company");

      for(cCustomer cust : ListCust)
      {
        cb_company.addItem(cust.mgetId);
        cb_company.setItemcaption(cust.mgetId, cust.mgetName);
      }


    }

    class cCustomer()
    {
      private String name;
      private String Id;

      public String GetName() 
      {
        return this.name
      }

       // Same for id

    }

I tried checking the value is there and setting it but nothing happens. I looked up but couldn't find an answer

我尝试检查该值是否存在并进行设置,但没有任何反应。我抬头但找不到答案

if(cb_company.getItemCaption(value) != null)
    cb_company.set(value);

采纳答案by wypieprz

Assuming that your ComboBoxuses single select mode, you can select a given item programatically with

假设您ComboBox使用单选模式,您可以使用编程方式选择给定的项目

cb_company.select(value)

where valuepoints to cCustomer.Id. So the code may look as follows:

value指向哪里cCustomer.Id。所以代码可能如下所示:

cb_company = new ComboBox("Company");

for(cCustomer cust : ListCust)
{
    cb_company.addItem(cust.mgetId);
    cb_company.setItemcaption(cust.mgetId, cuts.mgetName);
}

//select the first item from the container
cb_company.select(ListCust.get(0));