java - 如何删除组合框中选定的值

标签 java combobox vaadin

我想删除 ComboBox 中的所有项目,并在单击另一个按钮时用其他一些项目重新填充它。我使用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();
            }


        }

    });

最佳答案

如果您希望对您的国家/地区组合框所做的更改立即生效,则必须使用:

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

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

关于java - 如何删除组合框中选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16791388/

相关文章:

java - FizzBu​​zz 在 Java 中使用方法

Java:如何从终端构建和使用包

c# - Wpf ComboBox 选定项对齐

java - 构造函数与 setter 注入(inject)

java - 为什么使用装箱和加宽的方法重载会产生不明确的错误?

java - 什么是 Swing-相当于 HTML <optgroup>

c# - 如何在 C# winforms 中获取 ComboBox 的 "drop down button"大小

css - Vaadin 使用 CSS 在 v.tabsheet 中分隔标签

java - Vaadin 从按钮点击重定向到 URL

5000~ 用户的 Java Web 应用程序