We help IT Professionals succeed at work.

Java code to populate the world country names in a combobox

Short Java code to populate the world country names in a combobox.  
Comment
Watch Question

Author

Commented:
How do I populate in Java the world country names in a combobox without using a database table.  I understand that Java has this built in list.
Awarded 2011
Awarded 2011

Commented:
I'm not sure wht you mean by this qustion, but
Let's suppose you have World counties in the ArrayList

JComboBox has simplest constructor which takes Vector, so:

ArrayList<String) countries;
Vector<String> v = new Vector<String>();

for(String s: countries){

v.addElement(s);
}

JComboBox cb = new JComboBox(v);

Open in new window

Author

Commented:
I understand that Java has this built in country list the thing is to access it and put it in a combo box.  The attached code I found on the internet but it only prints it to screen using system.out.println() method.  Also it prints out 3 columns the first 2 I dont need.  Do you have a simpler way?  
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class CountryList {
  public static void main(String[] args) {
    List<Country> countries = new ArrayList<Country>();

    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
      String iso = locale.getISO3Country();
      String code = locale.getCountry();
      String name = locale.getDisplayCountry();

      if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
        countries.add(new Country(iso, code, name));
      }
    }

    Collections.sort(countries, new CountryComparator());
    for (Country country : countries) {
      System.out.println(country);
    }
  }
}

class CountryComparator implements Comparator<Country> {
  private Comparator comparator;

  CountryComparator() {
    comparator = Collator.getInstance();
  }

  public int compare(Country o1, Country o2) {
    return comparator.compare(o1.name, o2.name);
  }
}

class Country {
  private String iso;

  private String code;

  public String name;

  Country(String iso, String code, String name) {
    this.iso = iso;
    this.code = code;
    this.name = name;
  }

  public Country(String name2) {
	// TODO Auto-generated constructor stub
}

public String toString() {
    return iso + " - " + code + " - " + name.toUpperCase();
  }
}

Open in new window

Awarded 2011
Awarded 2011
Commented:
Vector<String> v = new Vector<String>();

 Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
      //String iso = locale.getISO3Country();
      //String code = locale.getCountry();
      String name = locale.getDisplayCountry();
  if(!v.contains(name)) v.addElement(name);
  }


JComboBox cb = new JComboBox(v);

Open in new window



Author

Commented:
Thanks!  I am trying the solution now and will report back shortly.
Awarded 2011
Awarded 2011
Commented:
You probably also want to say

Collections.sort(v);

before

JComboBox cb = new JComboBox(v);

to make them alphabaetical

Author

Commented:
Thanks very much.  That is the solution.  However, I thought that the builtin ISOcountry would have a listing of over 200 countries but the list is small.  Many countries are missing.  Do you know if java has a builting method with over 200 countries.  Or do you have a table that I can populate?
Awarded 2011
Awarded 2011

Commented:

Author

Commented:
However, I thought that the builtin ISOcountry would have a listing of over 200 countries but the list is small.  Many countries are missing.  Do you know if java has a builting method with over 200 countries.  Or do you have a table that I can populate?
Awarded 2011
Awarded 2011

Commented:
This list which I posed above has 242 countries

Author

Commented:
This sight does not have a download option.  How would you suggest i do that. other than copy function.  

Author

Commented:
I now realize that it is txt file and downloaded it.
Awarded 2011
Awarded 2011

Commented:

Author

Commented:
I alread downloaded the file and export it in a dbf table.  Solution complete.  You are good.