简体   繁体   中英

How to remove null value from ValueListBox values

I am new to GWT. I am writing a simple GWT program where I need to use a combo box for which I used an instance of ValueListBox . In that combo, I need to list out the numbers from 1 to 12 representing the months of a year. But the combo appends null value at the end. Can anyone please help me how to remove that null value ?

    final ValueListBox<Integer> monthCombo = new ValueListBox<Integer>(new Renderer<Integer>() {

            @Override
            public String render(Integer object) {
                return String.valueOf(object);
            }

            @Override
            public void render(Integer object, Appendable appendable) throws IOException {
                if (object != null) {

                    String value = render(object);
                    appendable.append(value);
                }
            }
        });
    monthCombo.setAcceptableValues(getMonthList());
    monthCombo.setValue(1);

    private List<Integer> getMonthList() {
        List<Integer> list = new ArrayList<Integer>();

        for (int i = 1; i <= 12; i++) {
            list.add(i);
        }

        return list;
    }

在此输入图像描述

Call setValue before setAcceptableValues .

The reason is that the value is null when you call setAcceptableValues , and ValueListBox automatically adds any value (generally passed to setValue ) to the list of acceptable values (so that the value is actually set , and can be selected by the user, and re-selected if she selected another value and wants to go back to the original one). Calling setValue first with a value that will be in the list of acceptable values negates this side-effect.

See http://code.google.com/p/google-web-toolkit/issues/detail?id=5477

Quoting from this question :

Beware the setAcceptableValues automatically adds the current value (returned by getValue, and defaults to null) to the list (and setValue automatically adds the value to the list of acceptable values too if needed)

So try inversing the order in which you call setValue and setAcceptableValues as following:

monthCombo.setValue(1);
monthCombo.setAcceptableValues(getMonthList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM