Java Swing Country List ComboBox Example
- Details
- Written by Nam Ha Minh
- Last Updated on 08 November 2019   |   Print Email
In this tutorial, I will guide you to write Java code displaying a dropdown list that allows the user to select a country/region in a Swing application, as shown below:
Note that this country list is generated – not manually created – based on the internationalization (i18n) API in Java:
String[] countryCodes = java.util.Locale.getISOCountries(); for (String countryCode : countryCodes) { Locale locale = new Locale("", countryCode); String code = locale.getCountry(); String name = locale.getDisplayCountry(); // use country code and country name... }
In this way, the country list is generated based on ISO standard of country codes and country names, which is safe and convenient to use.
First, create the Country class as follows:
package net.codejava; public class Country implements Comparable<Country> { private String code; private String name; public Country(String code, String name) { this.code = code; this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return this.name; } @Override public int compareTo(Country anotherCountry) { return this.name.compareTo(anotherCountry.getName()); } }
Then code the following method that returns a list (an array) of Country objects:
private Country[] createCountryList() { String[] countryCodes = Locale.getISOCountries(); Country[] listCountry = new Country[countryCodes.length]; for (int i = 0; i < countryCodes.length; i++) { Locale locale = new Locale("", countryCodes[i]); String code = locale.getCountry(); String name = locale.getDisplayCountry(); listCountry[i] = new Country(code, name); } Arrays.sort(listCountry); return listCountry; }
Note that Locale.getISOCountries() method returns an array of country codes in alphabetic order, so we need to sort the Country array by country name:
Arrays.sort(listCountry);
The array is sorted based on country name in alphabetic order because the Country class implements the compareTo() method:
public class Country implements Comparable<Country> { private String code; private String name; ... @Override public int compareTo(Country anotherCountry) { return this.name.compareTo(anotherCountry.getName()); } }
Then we can create a JComboBox in Swing to for the country list like this:
Country[] listCountry = createCountryList(); JComboBox<Country> comboCountry = new JComboBox<>(listCountry);
And get the country selected by the user:
Country selectedCountry = (Country) comboCountry.getSelectedItem();
For your reference, below is the full code of a Java Swing program that shows a country dropdown list:
/** * Java Swing program - demo for country list * (C) Copyright by CodeJava.net * @author Nam Ha Minh */ package net.codejava; import java.awt.*; import java.util.*; import javax.swing.*; public class CountryListDemo extends JFrame implements ActionListener { private JLabel labelCountry; private JComboBox<Country> comboCountry; private JButton buttonSubmit; public CountryListDemo() throws HeadlessException { super("Country List Demo"); initComponents(); setSize(600, 250); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void initComponents() { labelCountry = new JLabel("Select your country: "); Country[] listCountry = createCountryList(); comboCountry = new JComboBox<>(listCountry); buttonSubmit = new JButton("Submit"); buttonSubmit.addActionListener(this); setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); add(labelCountry); add(comboCountry); add(buttonSubmit); } private Country[] createCountryList() { String[] countryCodes = Locale.getISOCountries(); Country[] listCountry = new Country[countryCodes.length]; for (int i = 0; i < countryCodes.length; i++) { Locale locale = new Locale("", countryCodes[i]); String code = locale.getCountry(); String name = locale.getDisplayCountry(); listCountry[i] = new Country(code, name); } Arrays.sort(listCountry); return listCountry; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CountryListDemo().setVisible(true); } }); } @Override public void actionPerformed(ActionEvent e) { Country selectedCountry = (Country) comboCountry.getSelectedItem(); JOptionPane.showMessageDialog(this, "You selected country: " + selectedCountry); } }
Run this program and it appears like this:
Select a country from the dropdown list and click Submit, it says:
That’s how to create a dropdown list showing countries and regions with Java Swing.
Other Java Swing Tutorials:
- JComboBox basic tutorial and examples
- Java Swing Hello World Tutorial for Beginners Using Text Editor
- JFrame basic tutorial and examples
- JPanel basic tutorial and examples
- JLabel basic tutorial and examples
- JTextField basic tutorial and examples
- JCheckBox basic tutorial and examples
- JList basic tutorial and examples
- JTree basic tutorial and examples
- A Simple JTable Example for Display
Comments