SelectOneListbox is used to choose a single item from a list.
<div class="card">
<h:form>
<h5 class="p-mt-0">Basic</h5>
<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>
<h5>Advanced</h5>
<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>
</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 {
private int id;
private String name;
private String code;
public Country() {}
public Country(int id, String name, String code) {
this.id = id;
this.name = name;
this.code = code;
}
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;
}
@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;
}
}
@Named
@ApplicationScoped
public class CountryService {
private List<Country> countries;
@PostConstruct
public void init() {
countries = new ArrayList<>();
String[] locales = Locale.getISOCountries();
for (int i = 0; i < locales.length; i++) {
Locale country = new Locale("", locales[i]);
countries.add(new Country(i, country.getDisplayCountry(), country.getCountry().toLowerCase()));
}
Collections.sort(countries, (Country c1, Country c2) -> c1.getName().compareTo(c2.getName()));
}
public List<Country> getCountries() {
return new ArrayList<>(countries);
}
}