2013-10-10 128 views
3

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

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 

    } 

我试着检查值是否存在并设置它,但没有任何反应。我抬起头,却找不到答案

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

回答

8

假设你ComboBox使用单一选择模式,可以选择编程给定项目与

cb_company.select(value) 

其中valuecCustomer.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)); 
+0

谢谢,我试图与名称设置... SMH – Br0thazS0ul

+0

不是为我工作,甚至当我固定的ID为10,例如 – Bourkadi