Search code examples
javajavafxcomboboxiso

How do I display all country list in ComboBox? [JavaFX]


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.


Solution

  • 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);
     }