Codename One - addActionListener of a Picker

The following code, in a lot of version of Android and iOS, shows the options "Cancel" and "OK". I would like that the addActionListener code is executed only when the user taps on the "OK" button. At the moment, that code is executed even if the user taps the "Cancel" button:

    Form hi = new Form("Hi World", BoxLayout.y());
    Picker languagePicker = new Picker();
    languagePicker.setType(Display.PICKER_TYPE_STRINGS);
    languagePicker.setStrings("Italian", "English", "German");
    languagePicker.setSelectedString("English");
    languagePicker.addActionListener((ev) -> {
        String newLanguage = languagePicker.getSelectedString();
        if (newLanguage != null && newLanguage.length() > 0) {
            Log.p("Language selected: " + newLanguage);
        }
    });
    hi.add(languagePicker);
    hi.show();

1 answer

  • answered 2018-02-13 05:07 Shai Almog

    The Picker is a native component. As a result we just don't have that level of access or consistency when working with it. Keep in mind that in some forms it won't even have a list of elements so our control is very limited.