2013-05-28 3 views
0

Я хочу удалить все элементы в ComboBox и пополнить его некоторыми другими элементами, когда нажимает другая кнопка. Я делаю это с помощью removeAll(), но ранее выбранное значение все еще присутствует в комбо. Это то, что я сделал.Как удалить выбранное значение в ComboBox

@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

Это в веб-приложении или что? – MaheshVarma

+0

@MaheshVarma да, это в веб-приложении –

ответ

1

Если вы хотите, чтобы изменения в стране-ComboBox вступают в силу немедленно, чем вы должны использовать:

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

Таким образом, код в ValueChangeListener будет выполняться немедленно. Затем ваш список областей будет очищен. Вы должны воздерживаться от установки всего на «немедленный», поскольку это может вызвать чрезмерный трафик.

+0

@Sanjaya О, и в следующий раз, пожалуйста, дайте runnable пример вашего кода. Это проще и быстрее ответить на ваши вопросы, когда вы можете просто скопировать материал пасты, чтобы проверить его. – 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)); 
} 
Смежные вопросы