2013-05-28 25 views
0

我想删除组合框中的所有项目,并在另一个按钮单击时用其他项目填充它。我使用removeAll()方法执行此操作,但之前选定的值仍然存在于组合框中。这就是我所做的。如何删除组合框中的选定值

@AutoGenerated 
private AbsoluteLayout mainLayout; 
@AutoGenerated 
private GridLayout gridLayout_1; 
@AutoGenerated 
private HorizontalLayout horizontalLayout_1; 
@AutoGenerated 
private Button cancelBtn; 
@AutoGenerated 
private Button saveBtn; 
@AutoGenerated 
private TextField openingHoursTf; 
@AutoGenerated 
private Label label_10; 
@AutoGenerated 
private TextField emailTf; 
@AutoGenerated 
private Label label_9; 
@AutoGenerated 
private TextField phoneTf; 
@AutoGenerated 
private Label label_3; 
@AutoGenerated 
private TextField postCodeTf; 
@AutoGenerated 
private Label label_4; 
@AutoGenerated 
private TextArea addressLine2Tf; 
@AutoGenerated 
private Label label_8; 
@AutoGenerated 
private TextArea addressLine1Tf; 
@AutoGenerated 
private Label label_2; 
@AutoGenerated 
private TextArea descriptionTf; 
@AutoGenerated 
private Label label_5; 
@AutoGenerated 
private TextField nameTf; 
@AutoGenerated 
private Label label_1; 
@AutoGenerated 
private ComboBox statusComboBox; 
@AutoGenerated 
private Label label_21; 
@AutoGenerated 
private ComboBox typeComboBox; 
@AutoGenerated 
private Label label_19; 
@AutoGenerated 
private TextField codeTextField; 
@AutoGenerated 
private Label label_17; 
@AutoGenerated 
private ComboBox areaComboBox; 
@AutoGenerated 
private Label label_15; 
@AutoGenerated 
private ComboBox stateComboBox; 
@AutoGenerated 
private Label label_13; 
@AutoGenerated 
private ComboBox countryComboBox; 
@AutoGenerated 
private Label label_11; 
@AutoGenerated 
private Upload image_upload_1; 
@AutoGenerated 
private Label label_6; 
/** 
* The constructor should first build the main layout, set the composition 
* root and then do any custom initialization. 
* 
* The constructor will not be automatically regenerated by the visual 
* editor. 
*/ 

private Window window; 
private StoreDTO store; 
private StoreDataProvider storeDataProvider; 
private StoreContainer storeContainer; 
List<CountryDTO> countries = null; 
List<StoreTypeDTO> storeTypeList = null; 
List<AreaDTO> areasList=null; 
String imageMediumUrl; 
String imageHighUrl; 
String imageLowUrl; 
private ImageUploader uploader; 

public NewStoreWindow() { 
    buildMainLayout(); 
    setCompositionRoot(mainLayout); 
    statusComboBox.addItem(Status.ACTIVE); 
    statusComboBox.addItem(Status.INACTIVE); 
    statusComboBox.setNullSelectionAllowed(false); 

    try { 
     countries = StoreDataProvider.getStoreDataProvider() 
       .getAllCountries(); 
    } catch (Exception e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    for (CountryDTO country : countries) { 
     countryComboBox.addItem(country); 


    } 
    if (countries != null && !countries.isEmpty()) { 
     countryComboBox.select(countries.get(0)); 
    } 
    countryComboBox.setNullSelectionAllowed(false); 
    CountryDTO dto=(CountryDTO)countryComboBox.getValue(); 

    try { 
     areasList=StoreDataProvider.getStoreDataProvider().getAreasByCountry(dto.getId()); 
     areaComboBox.removeAllItems(); 
     for (AreaDTO area : areasList) { 
      areaComboBox.addItem(area); 

     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    areaComboBox.setNullSelectionAllowed(false); 
    countryComboBox.addListener(new ValueChangeListener() { 

     @Override 
     public void valueChange(ValueChangeEvent event) { 
      areaComboBox.removeAllItems(); 
      areaComboBox.setValue(null); 
      CountryDTO dto=(CountryDTO)countryComboBox.getValue(); 

      try { 
       areasList=StoreDataProvider.getStoreDataProvider().getAreasByCountry(dto.getId()); 

       for (AreaDTO area : areasList) { 
        areaComboBox.addItem(area); 

       } 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 

    }); 
+0

你能提供更多的代码吗?起初看起来很难看,因为它不清楚'CountryComboBox'和'areaComboBox'的创建位置。 – RishikeshDhokare

+0

它是在一个web应用程序或什么? – MaheshVarma

+0

@MaheshVarma是的,它是在一个web应用程序 –

回答

1

如果你想有改变你的国家组合框比你要使用即时生效:

countryComboBox = new ComboBox(); 
countryComboBox.setImmediate(true); 

这样,ValueChangeListener内的代码将被立即执行。您的区域清单将被清除。您应该避免将所有内容设置为“立即”,因为这可能会导致流量过大。

+0

@Sanjaya哦,下一次请给出一个可运行的代码示例。当您只需复制粘贴内容以进行测试时,回答您的问题会更轻松快捷。 – DKM

+0

感谢兄弟。它工作得很好。再次感谢您的帮助 –

1

正如你已经完成setNullSelectionAllowed(false) combox框必须始终有一个值。 重新填充列表和组合框,将值设置为列表中的第一个dto。

例如

areaComboBox.removeAllItems(); 
CountryDTO dto = (CountryDTO)countryComboBox.getValue(); 

areasList = StoreDataProvider.getStoreDataProvider().getAreasByCountry(dto.getId()); 

for (AreaDTO area : areasList) { 
    areaComboBox.addItem(area); 
} 

// Set value to the first of the list  
if(!areaList.isEmpty()){ 
    areaComboBox.setValue(areasList.get(0)); 
}