SelectOneListbox
SelectOneListbox is used to choose a single item from a list.
Documentation
<style type="text/css">
.label, .value {
vertical-align: top
}
.output {
font-weight: bold
}
</style>
<h:form>
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5" columnClasses="label, value">
<p:outputLabel for="basic" value="Basic:" />
<p:selectOneListbox id="basic" value="#{selectOneView.option}">
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
</p:selectOneListbox>
<p:outputLabel for="advanced" value="Advanced:" />
<p:selectOneListbox id="advanced" value="#{selectOneView.theme}" converter="#{themeConverter}" var="t" filter="true" filterMatchMode="contains">
<f:selectItems value="#{selectOneView.themes}" var="theme" itemLabel="#{theme.displayName}" itemValue="#{theme}" />
<p:column>
<h:graphicImage name="showcase/images/themeswitcher/themeswitcher-#{t.name}.png" alt="#{t.name}" styleClass="ui-theme" />
</p:column>
<p:column>
<h:outputText value="#{t.displayName}" />
</p:column>
</p:selectOneListbox>
</h:panelGrid>
<p:separator />
<p:commandButton value="Submit" update="display" oncomplete="PF('dlg').show()" icon="pi pi-check" />
<p:dialog header="Selected Values" modal="true" showEffect="fade" widgetVar="dlg" resizable="false">
<p:panelGrid columns="2" id="display" columnClasses="label,output">
<h:outputText value="Basic:" />
<h:outputText value="#{selectOneView.option}" />
<h:outputText value="Advanced:" />
<h:outputText value="#{selectOneView.theme}" />
</p:panelGrid>
</p:dialog>
</h:form>
@Named
@RequestScoped
public class SelectOneView {
private String option;
private Theme theme;
private List<Theme> themes;
@Inject
private ThemeService service;
@PostConstruct
public void init() {
themes = service.getThemes();
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public Theme getTheme() {
return theme;
}
public void setTheme(Theme theme) {
this.theme = theme;
}
public List<Theme> getThemes() {
return themes;
}
public void setService(ThemeService service) {
this.service = service;
}
}
public class Theme {
private int id;
private String displayName;
private String name;
public Theme() {}
public Theme(int id, String displayName, String name) {
this.id = id;
this.displayName = displayName;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
@Named
@ApplicationScoped
public class ThemeService {
private List<Theme> themes;
@PostConstruct
public void init() {
themes = new ArrayList<>();
themes.add(new Theme(0, "Nova-Light", "nova-light"));
themes.add(new Theme(1, "Nova-Dark", "nova-dark"));
themes.add(new Theme(2, "Nova-Colored", "nova-colored"));
themes.add(new Theme(3, "Luna-Blue", "luna-blue"));
themes.add(new Theme(4, "Luna-Amber", "luna-amber"));
themes.add(new Theme(5, "Luna-Green", "luna-green"));
themes.add(new Theme(6, "Luna-Pink", "luna-pink"));
themes.add(new Theme(7, "Omega", "omega"));
}
public List<Theme> getThemes() {
return themes;
}
}