0
public void start(final Stage frame) throws Exception {
    String[] locales = Locale.getISOCountries();
    for (String countrylist : locales) {
        Locale obj = new Locale("", countrylist);
        String[] city = {obj.getDisplayCountry()};
        for (int x = 0; x < city.length; x++) {
            cities = FXCollections.observableArrayList(obj.getDisplayCountry());
            country = new ComboBox<String>(cities);
        }
    }
}

I want to display the country list using Locale class. However I only manage to display one country in the Combobox when I run the code. I am not sure whether I got the loop wrong or what.

monolith52
  • 886
  • 2
  • 6
  • 9

4 Answers4

1

you are creating a new combo box every loop, try creating it outside the loops and only fill it inside?

Hope that helps.

Greetings,

Gian-Marco

Gian-Marco
  • 23
  • 7
1

Use This code

  public void start(Stage primaryStage) throws Exception {

                ObservableList<String> cities = FXCollections.observableArrayList();
                ComboBox<String> country = new ComboBox<String>(cities);

                String[] locales1 = Locale.getISOCountries();
                for (String countrylist : locales1) {
                    Locale obj = new Locale("", countrylist);
                    String[] city = { obj.getDisplayCountry() };
                    for (int x = 0; x < city.length; x++) {
                        cities.add(obj.getDisplayCountry());
                    }
                }
                country.setItems(cities);
 }
Keyur Bhanderi
  • 1,524
  • 1
  • 11
  • 17
  • If you are answearing question then at least use correct naming convention. What does `aaa` class means?? OP posted enough code to solve his problem, then why you are creating example that is not related to provided code? – MBec Apr 28 '17 at 07:54
0

You are creating new ComboBox in every iteration, that is why you always get ComboBox with single country(last country in list). You can use Stream to get all countries.

ObservableList<String> countries = Stream.of(Locale.getISOCountries())
        .map(locales -> new Locale("", locales))
        .map(Locale::getDisplayCountry)
        .collect(Collectors.toCollection(FXCollections::observableArrayList));

ComboBox<String> cb = new ComboBox<>(countries);
MBec
  • 2,172
  • 1
  • 12
  • 15
0

here you get the list

    ComboBox<String> country = new ComboBox<>();
    String[] locales = Locale.getISOCountries();
    for (String countrylist : locales) {
        Locale obj = new Locale("", countrylist);
        String[] city = {obj.getDisplayCountry()};
        country.setItems(FXCollections.observableArrayList(locales));
    }
Chris
  • 72
  • 1
  • 8