Class CountryPicker

All Implemented Interfaces:
Animation, Editable, ActionSource<ActionEvent>, StyleListener, IconHolder, ReleasableComponent, SelectableIconHolder, TextHolder

public class CountryPicker extends Picker

A searchable Picker (Picker#setSearchable(boolean)) of countries: each row shows the flag, the English name and the international dialing code, and typing filters on any of them.

CountryPicker country = new CountryPicker();
country.setSelectedCountryCode("FR");
country.addActionListener(e -> Log.p(country.getSelectedCountryCode() + " " + country.getDialingCode()));

Countries are identified by their ISO 3166-1 alpha-2 code. Flags are Unicode regional indicator pairs, so they show where the platform font has flag emoji and read as two letters elsewhere. The UIIDs are those of a searchable Picker. The table is the one PhoneNumberField uses (PhoneNumberField#getAllCountries()), and #getSelectedCountry() answers its PhoneNumberField.Country.

  • Constructor Details

    • CountryPicker

      public CountryPicker()
      A country picker with the hint "Country".
    • CountryPicker

      public CountryPicker(String hint)

      A country picker.

      Parameters
      • hint: the text shown while nothing is picked, and the sheet's title
  • Method Details

    • getCountryCodes

      public static String[] getCountryCodes()

      Every country code the picker lists, in the order of the English names.

      Returns

      a new array of ISO 3166-1 alpha-2 codes

    • getCountryName

      public static String getCountryName(String code)

      The English name of a country.

      Parameters
      • code: an ISO 3166-1 alpha-2 code, in either case
      Returns

      the name, or null for a code the picker does not list

    • getDialingCode

      public static String getDialingCode(String code)

      The international dialing code of a country, such as +44.

      Parameters
      • code: an ISO 3166-1 alpha-2 code, in either case
      Returns

      the dialing code with its plus sign, or null for an unknown code

    • getFlag

      public static String getFlag(String code)

      The flag emoji of a country: its two regional indicator symbols.

      Parameters
      • code: a two letter code
      Returns

      the flag as a string of two surrogate pairs, or an empty string when code is not two ASCII letters

    • getSelectedCountry

      public PhoneNumberField.Country getSelectedCountry()

      The selected country as an entry of the shared country table.

      Returns

      the selected country, or null when none is selected

    • getSelectedCountryCode

      public String getSelectedCountryCode()

      The picked country.

      Returns

      its ISO 3166-1 alpha-2 code, or null when nothing is picked

    • setSelectedCountryCode

      public void setSelectedCountryCode(String code)

      Picks a country without notifying listeners.

      Parameters
      • code: an ISO 3166-1 alpha-2 code, or null to clear
    • getDialingCode

      public String getDialingCode()

      The dialing code of the picked country, such as +33.

      Returns

      the code with its plus sign, or null when nothing is picked

    • bindPhoneField

      public void bindPhoneField(TextField phone)

      Fills phone with the picked country's dialing code and a space each time the user picks a country, so the user types only the local part.

      Parameters
      • phone: the phone number field to keep in step
    • setOptions

      public void setOptions(String... options)

      Replaces the shown countries, for instance to offer only the countries an app ships to. The selection maps back to a code by its label, so pass labels as #getCountryLabel(String) returns them; a label that is not a country's makes #getSelectedCountryCode() answer null when picked.

      Parameters
      • options: the country labels to show
    • getCountryLabel

      public static String getCountryLabel(String code)

      The label the picker shows for a country: its flag, English name and dialing code.

      Parameters
      • code: an ISO 3166-1 alpha-2 code, in either case
      Returns

      the label, or null for a code the picker does not list