SelectOneListbox is used to choose a single item from a list.
<div class="card">
<h:form>
<h5>Basic</h5>
<div class="p-col-12 p-md-6 p-lg-4 p-xl-3">
<p:selectOneListbox id="basic" value="#{selectOneView.option}">
<f:selectItem itemLabel="New York" itemValue="New York"/>
<f:selectItem itemLabel="Rome" itemValue="Rome"/>
<f:selectItem itemLabel="London" itemValue="London"/>
<f:selectItem itemLabel="Istanbul" itemValue="Istanbul"/>
<f:selectItem itemLabel="Paris" itemValue="Paris"/>
</p:selectOneListbox>
</div>
<h5>Advanced</h5>
<div class="p-col-12 p-md-6 p-lg-4 p-xl-3">
<p:selectOneListbox id="advanced" value="#{selectOneView.country}" converter="#{countryConverter}"
var="c" filter="true" filterMatchMode="contains">
<f:selectItems value="#{selectOneView.countries}" var="country" itemLabel="#{country.name}" itemValue="#{country}"/>
<p:column>
<span class="flag flag-#{c.code}" style="width: 30px; height: 20px"/>
</p:column>
<p:column>
<h:outputText value="#{c.name}"/>
</p:column>
</p:selectOneListbox>
</div>
</h:form>
</div>
@Named
@RequestScoped
public class SelectOneView {
private String option;
private Country country;
private List<Country> countries;
@Inject
private CountryService service;
@PostConstruct
public void init() {
countries = service.getCountries();
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
public void setService(CountryService service) {
this.service = service;
}
}
public class Country implements Serializable, Comparable<Country> {
private int id;
private String name;
private String code;
private Locale locale;
private boolean rtl;
public Country() {
}
public Country(int id, Locale locale) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
}
public Country(int id, Locale locale, boolean rtl) {
this(id, locale.getDisplayCountry(), locale.getCountry().toLowerCase(), locale);
this.rtl = rtl;
}
public Country(int id, String name, String code) {
this(id, name, code, null);
}
public Country(int id, String name, String code, Locale locale) {
this.id = id;
this.name = name;
this.code = code;
this.locale = locale;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public String getLanguage() {
return locale == null ? "en" : locale.getLanguage();
}
public String getDisplayLanguage() {
return locale == null ? "English" : locale.getDisplayLanguage();
}
public boolean isRtl() {
return rtl;
}
public void setRtl(boolean rtl) {
this.rtl = rtl;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Country country = (Country) o;
return id == country.id
&& Objects.equals(name, country.name)
&& Objects.equals(code, country.code);
}
@Override
public int hashCode() {
return Objects.hash(id, name, code);
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Country o) {
return name.compareTo(o.name);
}
}
@Named
@ApplicationScoped
public class CountryService {
private List<Country> countries;
private Map<Integer, Country> countriesAsMap;
private List<Country> locales;
private Map<Integer, Country> localesAsMap;
@PostConstruct
public void init() {
countries = new ArrayList<>();
locales = new ArrayList<>();
String[] isoCodes = Locale.getISOCountries();
for (int i = 0; i < isoCodes.length; i++) {
Locale locale = new Locale("", isoCodes[i]);
countries.add(new Country(i, locale));
}
Collections.sort(countries, (Country c1, Country c2) -> c1.getName().compareTo(c2.getName()));
int i = 0;
locales.add(new Country(i++, Locale.US));
locales.add(new Country(i++, Locale.FRANCE));
locales.add(new Country(i++, Locale.GERMANY));
locales.add(new Country(i++, Locale.ITALY));
locales.add(new Country(i++, Locale.KOREA));
locales.add(new Country(i++, new Locale("es", "ES")));
locales.add(new Country(i++, new Locale("ca", "ES")));
locales.add(new Country(i++, new Locale("nl", "NL")));
locales.add(new Country(i++, new Locale("pt", "BR")));
locales.add(new Country(i++, new Locale("pt", "PT")));
locales.add(new Country(i++, new Locale("ar", "SA"), true));
locales.add(new Country(i++, new Locale("cs", "CZ")));
locales.add(new Country(i++, new Locale("el", "GR")));
locales.add(new Country(i++, new Locale("fa", "IR"), true));
locales.add(new Country(i++, new Locale("hi", "IN")));
locales.add(new Country(i++, new Locale("in", "ID")));
locales.add(new Country(i++, new Locale("hr", "HR")));
locales.add(new Country(i++, new Locale("hu", "HU")));
locales.add(new Country(i++, new Locale("iw", "IL"), true));
locales.add(new Country(i++, new Locale("ka", "GE")));
locales.add(new Country(i++, new Locale("lt", "LT")));
locales.add(new Country(i++, new Locale("lv", "LV")));
locales.add(new Country(i++, new Locale("no", "NO")));
locales.add(new Country(i++, new Locale("pl", "PL")));
locales.add(new Country(i++, new Locale("ro", "RO")));
locales.add(new Country(i++, new Locale("ru", "RU")));
locales.add(new Country(i++, new Locale("sk", "SK")));
locales.add(new Country(i++, new Locale("sl", "SI")));
locales.add(new Country(i++, new Locale("sr", "RS")));
locales.add(new Country(i++, new Locale("sv", "SE")));
locales.add(new Country(i++, new Locale("tr", "TR")));
locales.add(new Country(i++, new Locale("uk", "UA")));
locales.add(new Country(i++, new Locale("vi", "VN")));
locales.add(new Country(i++, Locale.SIMPLIFIED_CHINESE));
locales.add(new Country(i++, Locale.TRADITIONAL_CHINESE));
}
public List<Country> getCountries() {
return new ArrayList<>(countries);
}
public Map<Integer, Country> getCountriesAsMap() {
if (countriesAsMap == null) {
countriesAsMap = getCountries().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return countriesAsMap;
}
public List<Country> getLocales() {
return new ArrayList<>(locales);
}
public Map<Integer, Country> getLocalesAsMap() {
if (localesAsMap == null) {
localesAsMap = getLocales().stream().collect(Collectors.toMap(Country::getId, country -> country));
}
return localesAsMap;
}
}