ToggleSwitch is used to select a boolean value.
<div class="card">
<h:form>
<p:growl id="msg"/>
<h5 class="mt-0">Basic</h5>
<p:toggleSwitch value="#{selectBooleanView.value1}"/>
<h5>Icon</h5>
<p:toggleSwitch value="#{selectBooleanView.value2}" onIcon="pi pi-check" offIcon="pi pi-times" />
<h5>Remote</h5>
<p:toggleSwitch value="#{selectBooleanView.value3}">
<p:ajax listener="#{selectBooleanView.addMessage}" update="msg"/>
</p:toggleSwitch>
</h:form>
</div>
@Named
@RequestScoped
public class SelectBooleanView {
private boolean value1;
private boolean value2;
private boolean value3;
public boolean isValue1() {
return value1;
}
public void setValue1(boolean value1) {
this.value1 = value1;
}
public boolean isValue2() {
return value2;
}
public void setValue2(boolean value2) {
this.value2 = value2;
}
public boolean isValue3() {
return value3;
}
public void setValue3(boolean value3) {
this.value3 = value3;
}
public void addMessage(AjaxBehaviorEvent event) {
UIComponent component = event.getComponent();
if (component instanceof UIInput) {
UIInput inputComponent = (UIInput) component;
Boolean value = (Boolean) inputComponent.getValue();
String summary = value ? "Checked" : "Unchecked";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(summary));
}
}
}