Primefaces Radio Button, Checkbox Example

Filed Under: JSF

Primefaces Radio Button and checkbox are select elements. Below are the primefaces components for different kinds of radio button and checkbox implementations.

  1. SelectBooleanButton
  2. SelectBooleanCheckbox
  3. SelectCheckboxMenu
  4. SelectManyButton
  5. SelectManyCheckbox
  6. SelectManyMenu
  7. SelectOneButton
  8. SelectOneListbox
  9. SelectOneMenu
  10. SelectOneRadio

Let’s explore these components thoroughly and see how can we leverage them into your application.

SelectBooleanButton Basic Info

SelectBooleanButton is used to select a binary decision with a toggle button.

Tag SelectBooleanButton
Component Class org.primefaces.component.selectbooleanbutton.SelectBooleanButton
Component Type org.primefaces.component.SelectBooleanButton
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectBooleanButtonRenderer
Renderer Class org.primefaces.component.selectbooleanbutton.SelectBooleanButtonRenderer

SelectBooleanButton Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null Method Expr A method expression that refers to a method validationg the input
valueChangeListener null Method Expr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.
onLabel null String Label to display when button is selected.
offLabel null String Label to display when button is unselected.
onIcon null String Icon to display when button is selected.
offIcon null String Icon to display when button is unselected.

Getting Started With SelectBooleanButton

selectBooleanButton.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:500px">
	<p:growl id="message"></p:growl>
	<p:outputLabel value="Turn your system:"></p:outputLabel>
		<p:selectBooleanButton offLabel="On" onLabel="Off" value="#{selectBooleanButton.status}" ></p:selectBooleanButton>
		<p:separator></p:separator>
	<p:commandButton value="Display System Status" action="#{selectBooleanButton.displaySystemStatus}" update="message"></p:commandButton>
</h:form>
</html>

SelectBooleanButton.java


package com.journaldev.prime.faces.beans;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectBooleanButton {
	private boolean status;

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String displaySystemStatus(){
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage("Your System Is: "+(status == true ? "Truned On":"Turned Off")));
		return "";
	}
}

Primefaces SelectBooleanButton - Turned Off View Primefaces SelectBooleanButton - Turned On View

SelectBooleanCheckbox Basic Info

SelectBooleanCheckbox is an extended version of the standard checkbox with theme integration.

Tag SelectBooleanCheckbox
Component Class org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckbox
Component Type org.primefaces.component.SelectBooleanCheckbox
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectBooleanCheckboxRenderer
Renderer Class org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckbox Renderer

SelectBooleanCheckbox Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.
itemLabel null String Label displayed next to checkbox.
tabindex null String Specifies tab order for tab key navigation.

Getting Started With SelectBooleanCheckbox

selectBooleanCheckbox.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:500px">
	<p:growl id="message"></p:growl>
	<p:outputLabel value="Turn your system:"></p:outputLabel>
		<p:selectBooleanCheckbox value="#{selectBooleanCheckbox.status}" ></p:selectBooleanCheckbox>
		<p:separator></p:separator>
	<p:commandButton value="Display System Status" action="#{selectBooleanCheckbox.displaySystemStatus}" update="message"></p:commandButton>
</h:form>
</html>

SelectBooleanCheckbox.java


package com.journaldev.prime.faces.beans;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectBooleanCheckbox {
	private boolean status;

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String displaySystemStatus(){
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage("Your System Is: "+(status == true ? "Truned On":"Turned Off")));
		return "";
	}
}

Primefaces SelectBooleanCheckbox - Turned Off View Primefaces SelectBooleanCheckbox - Turned On View

SelectBooleanCheckbox Client Side API

Widget: PrimeFaces.widget.SelectBooleanCheckbox

Method Params Return Type Description
check() void Checks the checkbox.
uncheck() void Unchecks the checkbox.
toggle() void Toggles check state.
  • PF implicit object is used for controlling the component from the client side. The expression used is PF('WidgetVarValue')

SelectCheckboxMenu Basic Info

SelectCheckboxMenu is a multi select component that displays options in an overlay.

Tag SelectCheckboxMenu
Component Class org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenu
Component Type org.primefaces.component.SelectCheckboxMenu
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectCheckboxMenuRenderer
Renderer Class org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer

SelectCheckboxMenu Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.
scrollHeight null Integer Height of the overlay.
onShow null String Client side callback to execute when overlay is displayed.
onHide null String Client side callback to execute when overlay is hidden.
filter false Boolean Renders an input field as a filter.
filterMatchMode startsWith String Match mode for filtering, valid values are startsWith, contains, endsWith and custom.
filterFunction null String Client side function to use in custom filtering.
caseSensitive false Boolean Defines if filtering would be case sensitive.
panelStyle null String Inline style of the overlay.
panelStyleClass null String Style class of the overlay.
appendTo null String Appends the overlay to the element defined by search expression. Defaults to document body.
tabindex null String Position of the element in the tabbing order.

Getting started with SelectCheckboxMenu

selectCheckboxMenu.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:outputLabel value="Select wanted tutorials:"></p:outputLabel>
		<p:selectCheckboxMenu value="#{selectCheckboxMenu.selectedTutorials}">
			<f:selectItems value="#{selectCheckboxMenu.tutorials}" var="tutorial" itemLabel="#{tutorial}" itemValue="#{tutorial}"></f:selectItems>
		</p:selectCheckboxMenu>
		<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectCheckboxMenu.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectCheckboxMenu.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectCheckboxMenu {

	private List<String> tutorials = new ArrayList<String>();

	private List<String> selectedTutorials = new ArrayList<String>();

	public SelectCheckboxMenu(){

	}

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public List<String> getSelectedTutorials() {
		return selectedTutorials;
	}

	public void setSelectedTutorials(List<String> selectedTutorials) {
		this.selectedTutorials = selectedTutorials;
	}

	public String register(){
		String message = "";
		for(String s : this.selectedTutorials){
			message = message + s + ",";
		}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", message));
		return "";
	}
}

Primefaces SelectCheckboxMenu - Initial ViewPrimefaces SelectCheckboxMenu - Register View

SelectCheckboxMenu Filtering

It’s possible for you to make use some sort of filtering against your viewed list. This mechanism will help you got a fast reach into the desired wanted item easily. To make your component applicable to filter its result, just follow below steps:

  • Make sure filter attribute is set to true.
  • Setting your filter mode; startsWith (by default), endsWith and custom.
  • In case you’ve set custom as a mode of your filter you must provide filterFunction attribute which normally associated with a JavaScript function.

selectCheckboxMenu.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:outputLabel value="Select wanted tutorials:"></p:outputLabel>
		<p:selectCheckboxMenu value="#{selectCheckboxMenu.selectedTutorials}" filter="true">
			<f:selectItems value="#{selectCheckboxMenu.tutorials}" var="tutorial" itemLabel="#{tutorial}" itemValue="#{tutorial}"></f:selectItems>
		</p:selectCheckboxMenu>
		<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectCheckboxMenu.register}" update="message"></p:commandButton>
</h:form>
</html>

Primefaces SelectCheckboxMenu - Filter View

SelectCheckboxMenu Ajax Behavior Events

Most of Primefaces’ components have associated in with a lot of Ajax events. The way in which those events can be leveraged is discussed deeply at Ajax Behavior Tutorial where you can get back into. Here’s, a detailed list of Ajax events that you can use against SelectCheckboxMenu.

Event Listener Parameter Fired
toggleSelect org.primefaces.event.ToggleSelectEvent When toggle all checkbox changes

SelectManyButton Basic Info

SelectManyButton is a multi select component using button UI.

Tag SelectManyButton
Component Class org.primefaces.component.selectmanybutton.SelectManyButton
Component Type org.primefaces.component.SelectManyButton
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectManyButton
Renderer Class org.primefaces.component.selectmanybutton.SelectManyButton

SelectManyButton Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.

Getting Started With SelectManyButton

selectManyButton.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectManyButton value="#{selectManyButton.selectedTutorials}">
		<f:selectItems value="#{selectManyButton.tutorials}"></f:selectItems>
	</p:selectManyButton>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectManyButton.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectManyButton.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectManyButton {
	private List<String> tutorials = new ArrayList<String>();

	private List<String> selectedTutorials = new ArrayList<String>();

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public List<String> getSelectedTutorials() {
		return selectedTutorials;
	}

	public void setSelectedTutorials(List<String> selectedTutorials) {
		this.selectedTutorials = selectedTutorials;
	}

	public String register(){
		String message = "";
		for(String s : this.selectedTutorials){
			message = message + s + ",";
		}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", message));
		return "";
	}
}

Primefaces SelectManyButton - Initial ViewPrimefaces SelectManyButton - Register View

SelectManyCheckbox Basic Info

SelectManyCheckbox is an extended version of the standard SelectManyCheckbox with theme integration.

Tag SelectManyCheckbox
Component Class org.primefaces.component.selectmanycheckbox.SelectManyCheckbox
Component Type org.primefaces.component.SelectManyCheckbox
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectManyCheckboxRenderer
Renderer Class org.primefaces.component.selectmanycheckbox.SelectManyCheckboxRenderer

SelectManyChcekbox Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/St ring An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
layout lineDirection String Layout of the checkboxes, valid values are lineDirection, pageDirection and grid.
columns 0 Integer Number of columns in grid layout.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.

Getting Started With SelectManyCheckbox

selectManyCheckbox.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectManyCheckbox value="#{selectManyCheckbox.selectedTutorials}">
		<f:selectItems value="#{selectManyCheckbox.tutorials}"></f:selectItems>
	</p:selectManyCheckbox>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectManyCheckbox.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectManyCheckbox.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectManyCheckbox {

	private List<String> tutorials = new ArrayList<String>();

	private List<String> selectedTutorials = new ArrayList<String>();

	public SelectManyCheckbox(){

	}

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public List<String> getSelectedTutorials() {
		return selectedTutorials;
	}

	public void setSelectedTutorials(List<String> selectedTutorials) {
		this.selectedTutorials = selectedTutorials;
	}

	public String register(){
		String message = "";
		for(String s : this.selectedTutorials){
			message = message + s + ",";
		}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", message));
		return "";
	}
}

Primefaces SelectManyCheckbox - Initial ViewPrimefaces SelectManyCheckbox - Register View

SelectManyMenu Basic Info

SelectManyMenu is an extended version of the standard SelectManyMenu.

Tag SelectManyMenu
Component Class org.primefaces.component.selectmanymenu.SelectManyMenu
Component Type org.primefaces.component.SelectManyMenu
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectManyMenuRenderer
Renderer Class org.primefaces.component.selectmanymenu.SelectManyMenuRenderer

SelectManyMenu Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
onclick null String Callback for click event.
ondblclick null String Callback for dblclick event.
style null String Inline style of the component.
styleClass null String Style class of the container.
tabindex null String Position of the input element in the tabbing order.
var null String Name of iterator to be used in custom content display.
showCheckbox false Boolean When true, a checkbox is displayed next to each item.
filter false Boolean Displays an input filter for the list.
filterMatchMode null String Match mode for filtering, valid values are startsWith (default), contains, endsWith and custom.
filterFunction null String Client side function to use in custom filterMatchMode.
caseSensitive false Boolean Defines if filtering would be case sensitive.
scrollHeight null Integer Defines the height of the scrollable area
converter null Converter/String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s static text, it must refer to a converter id

Getting Started With SelectManyMenu

selectManyMenu.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectManyMenu value="#{selectManyMenu.selectedTutorials}">
		<f:selectItems value="#{selectManyMenu.tutorials}"></f:selectItems>
	</p:selectManyMenu>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectManyMenu.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectManyMenu.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectManyMenu {

	private List<String> tutorials = new ArrayList<String>();

	private List<String> selectedTutorials = new ArrayList<String>();

	public SelectManyMenu(){

	}

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public List<String> getSelectedTutorials() {
		return selectedTutorials;
	}

	public void setSelectedTutorials(List<String> selectedTutorials) {
		this.selectedTutorials = selectedTutorials;
	}

	public String register(){
		String message = "";
		for(String s : this.selectedTutorials){
			message = message + s + ",";
		}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", message));
		return "";
	}
}

Primefaces SelectManyMenu - Initial ViewPrimefaces SelectManyMenu - Register View

SelectManyMenu – Custom Content

It’s possible for you to write in your own custom content for every single item. By providing whatever you want of Primefaces/jsf components that enclosed within SelectManyMenu open/end Tags, you can provide your own custom content. Following below sample shows you the same used example with a different images.

selectManyMenu.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectManyMenu value="#{selectManyMenu.selectedTutorials}" var="tutorial">
		<f:selectItems value="#{selectManyMenu.tutorials}"/>
		<p:column>
			<p:graphicImage value="/resources/images/#{tutorial}.jpg" width="40"></p:graphicImage>
		</p:column>
		<p:column>
			<p:outputLabel value="#{tutorial}"></p:outputLabel>
		</p:column>
	</p:selectManyMenu>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectManyMenu.register}" update="message"></p:commandButton>
</h:form>
</html>

Primefaces SelectManyMenu - Custom Content View

SelectManyMenu – Filtering

You can make use of filter functionality in SelectManyMenu same as the same usage by SelectCheckboxMenu.

SelectOneButton Basic Info

SelectOneButton is an input component to do a single select.

Tag SelectOneButton
Component Class org.primefaces.component.selectonebutton.SelectOneButton
Component Type org.primefaces.component.SelectOneButton
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectOneButtonRenderer
Renderer Class org.primefaces.component.selectonebutton.SelectOneButtonRenderer

SelectOneButton Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.
converter null Converter/St ring An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id

Getting Started With SelectOneButton

selectOneButton.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectOneButton value="#{selectOneButton.selectedTutorial}">
		<f:selectItems value="#{selectOneButton.tutorials}"></f:selectItems>
	</p:selectOneButton>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectOneButton.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectOneButton.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectOneButton {
	private List<String> tutorials = new ArrayList<String>();

	private String selectedTutorial = "";

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public String getSelectedTutorial() {
		return selectedTutorial;
	}

	public void setSelectedTutorial(String selectedTutorial) {
		this.selectedTutorial = selectedTutorial;
	}

	public String register(){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", this.selectedTutorial));
		return "";
	}
}

Primefaces SelectOneButton - Initial ViewPrimefaces SelectOneButton - Register View

SelectOneListbox Basic Info

SelectOneListbox is an extended version of the standard selectOneListbox component.

Tag SelectOneListbox
Component Class org.primefaces.component.selectonelistbox.SelectOneListbox
Component Type org.primefaces.component.SelectOneListbox
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectOneListboxRenderer
Renderer Class org.primefaces.component.selectonelistbox.SelectOneListBoxRenderer

SelectOneListbox Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate false Boolean When set true, process validations logic is executed at apply request values phase for this component.
required false Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Callback to execute on value change.
onclick null String Callback for click event.
ondblclick null String Callback for dblclick event.
style null String Inline style of the component.
styleClass null String Style class of the container.
tabindex null String Position of the input element in the tabbing order.
value null String Name of iterator to be used in custom content display.
var null String Name of iterator to be used in custom content display.
filter false Boolean Displays an input filter for the list.
filterMatchMode null String Match mode for filtering, valid values are startsWith (default), contains, endsWith and custom.
filterFunction null String Client side function to use in custom filterMatchMode.
caseSensitive false Boolean Defines if filtering would be case sensitive.
scrollHeight null Integer Defines the height of the scrollable area.

Getting Started With SelectOneListbox

selectOneListbox.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectOneListbox value="#{selectOneListBox.selectedTutorial}">
		<f:selectItems value="#{selectOneListBox.tutorials}"></f:selectItems>
	</p:selectOneListbox>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectOneListBox.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectOneListbox.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectOneListbox {
	private List<String> tutorials = new ArrayList<String>();

	private String selectedTutorial = "";

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public String getSelectedTutorial() {
		return selectedTutorial;
	}

	public void setSelectedTutorial(String selectedTutorial) {
		this.selectedTutorial = selectedTutorial;
	}

	public String register(){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", this.selectedTutorial));
		return "";
	}
}

Primefaces SelectOneListbox - Initial ViewPrimefaces SelectOneListbox - Register View

SelectOneMenu Basic Info

SelectOneMenu is an extended version of the standard SelectOneMenu.

Tag SelectOneMenu
Component Class org.primefaces.component.selectonemenu.SelectOneMenu
Component Type org.primefaces.component.SelectOneMenu
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectOneMenuRenderer
Renderer Class org.primefaces.component.selectonemenu.SelectOneMenuRenderer

SelectOneMenu Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate 0 Boolean When set true, process validations logic is executed at apply request values phase for this component.
required 0 Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
effect blind String Name of the toggle animation.
effectSpeed 400 Integer Duration of toggle animation in milliseconds.
disabled false Boolean Disables the component.
label null String User presentable name.
onchange null String Client side callback to execute on value change.
onkeyup null String Client side callback to execute on keyup.
onkeydown null String Client side callback to execute on keydown.
style null String Inline style of the component.
styleClass null String Style class of the container.
var null String Name of the item iterator.
height auto Integer Height of the overlay.
tabindex null String Tabindex of the input.
editable false Boolean When true, input becomes editable.
filter false Boolean Renders an input field as a filter.
filterMatchMode starts With String Match mode for filtering, valid values are startsWith, contains, endsWith and custom.
filterFunction null String Client side function to use in custom filtering.
caseSensitive false Boolean Defines if filtering would be case sensitive.
maxlength null Integer Number of maximum characters allowed in editable selectOneMenu.
appendTo null String Appends the overlay to the element defined by search expression. Defaults to document body.

Getting Started With SelectOneMenu

selectOneMenu.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form style="width:400px">
	<p:growl id="message" showDetail="true" showSummary="true"></p:growl>
	<p:selectOneMenu value="#{selectOneMenu.selectedTutorial}">
		<f:selectItems value="#{selectOneMenu.tutorials}"></f:selectItems>
	</p:selectOneMenu>
	<p:separator></p:separator>
	<p:commandButton value="Register" action="#{selectOneMenu.register}" update="message"></p:commandButton>
</h:form>
</html>

SelectOneMenu.java


package com.journaldev.prime.faces.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SelectOneMenu {
	private List<String> tutorials = new ArrayList<String>();

	private String selectedTutorial = "";

	@PostConstruct
	public void init(){
		this.tutorials = new ArrayList<String>();
		this.tutorials.add("Primefaces");
		this.tutorials.add("Hibernate");
		this.tutorials.add("Spring");
	}

	public List<String> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<String> tutorials) {
		this.tutorials = tutorials;
	}

	public String getSelectedTutorial() {
		return selectedTutorial;
	}

	public void setSelectedTutorial(String selectedTutorial) {
		this.selectedTutorial = selectedTutorial;
	}

	public String register(){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("You've Registered In:", this.selectedTutorial));
		return "";
	}
}

Primefaces SelectOneMenu - Register ViewPrimefaces SelectOneMenu - Initial View

SelectOneRadio Basic Info

Tag SelectOneRadio
Component Class org.primefaces.component.selectoneradio.SelectOneRadio
Component Type org.primefaces.component.SelectOneRadio
Component Family org.primefaces.component
Renderer Type org.primefaces.component.SelectOneRadioRenderer
Renderer Class org.primefaces.component.selectoneradio.SelectOneRadioRenderer

SelectOneRadio Attributes

Name Default Type Description
id null String Unique identifier of the component
rendered true Boolean Boolean value to specify the rendering of the component, when set to false component will not be rendered.
binding null Object An el expression that maps to a server side UIComponent instance in a backing bean
value null Object Value of the component referring to a List.
converter null Converter/ String An el expression or a literal text that defines a converter for the component. When it’s an EL expression, it’s resolved to a converter instance. In case it’s a static text, it must refer to a converter id
immediate 0 Boolean When set true, process validations logic is executed at apply request values phase for this component.
required 0 Boolean Marks component as required
validator null MethodExpr A method expression that refers to a method validationg the input
valueChangeListener null MethodExpr A method expression that refers to a method for handling a valuechangeevent
requiredMessage null String Message to be displayed when required field validation fails.
converterMessage null String Message to be displayed when conversion fails.
validatorMessage null String Message to be displayed when validation fields.
widgetVar null String Name of the client side widget.
disabled false Boolean Disables the component.
label null String User presentable name.
layout line Direction String Layout of the radiobuttons, valid values are lineDirection, pageDirection, custom and grid.
columns 0 Integer Number of columns in grid layout.
onchange null String Callback to execute on value change.
style null String Inline style of the component.
styleClass null String Style class of the container.
tabindex null String Specifies the tab order of element in tab navigation.
plain false Boolean Plain mode displays radiobuttons using native browser rendering instead of themes.

Getting Started With SelectOneRadio

SelectOneRadio usage is same as the using of SelectOneMenu with one difference is that a set of radio controls are rendered rather using of menu control.

Summary

All of Selective components are collected and explored in a one location. A detailed explanations, figures and examples are provided for all of them. Contribute us by commenting below and find downloaded source code.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages