Thodoris Bais

About Thodoris Bais

Thodoris Bais is currently working as a Junior Java Developer while on parallel, pursuing a diploma at Informatics & Telecommunications Engineering. He specializes in Java Core, Android and J2EE and looks forward to being a professional Software Engineer. He is also a quick learner and curious about new technologies.

CheckBox Example with JSF 2.0

Hi there, hope you had an interesting day. Today we ‘re gonna talk about checkboxes in JSF 2.0. To represent a checkbox in JSF, we use the tag h:selectBooleanCheckbox. Ok, that’s really easy, but what if we ‘d like to create a group of checkboxes, where the user could select more than one checkbox? This can be done using the h:selectManyCheckbox tag; the HTML renderings are exactly the same, as we saw in my previous example. To be more specific, before getting into the example’s structure, here is a small example that implements a group of three checkboxes, where the user can select more than one of them:

<h:selectManyCheckbox value="#{user.favNumber1}">
   	<f:selectItem itemValue="1" itemLabel="Number1 - 1" />
   	<f:selectItem itemValue="2" itemLabel="Number1 - 2" />
   	<f:selectItem itemValue="3" itemLabel="Number1 - 3" />

Ok, enough said, let’s have a quick example with that type of checkboxes, but I have to first notice the four different ways that we can populate a group of checkboxes:

  1. Hardcoded value in a f:selectItem tag.
  2. Generated values from an Array and passed into the fore-mentioned tag.
  3. Generated values using a Map and passed into the same tag.
  4. Generate values using an Object Array and passed again into the f:selectItem tag, then represent the value using a var attribute.

1. Backing Bean

Here’s the structure of the Bean that holds the submitted values.

package com.javacodegeeks.enterprise.jsf.checkboxes;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

public class UserBean implements Serializable{

	private static final long serialVersionUID = -3953324291794510390L;

	public boolean rememberMe;
	public String[] favoriteCar1;
	public String[] favoriteCar2;
	public String[] favoriteCar3;
	public String[] favoriteCar4;
	public boolean isRememberMe() {
		return rememberMe;
	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	public String[] getFavoriteCar1() {
		return favoriteCar1;
	public void setFavoriteCar1(String[] favoriteCar1) {
		this.favoriteCar1 = favoriteCar1;
	public String[] getFavoriteCar2() {
		return favoriteCar2;
	public void setFavoriteCar2(String[] favoriteCar2) {
		this.favoriteCar2 = favoriteCar2;
	public String[] getFavoriteCar3() {
		return favoriteCar3;
	public void setFavoriteCar3(String[] favoriteCar3) {
		this.favoriteCar3 = favoriteCar3;
	public String[] getFavoriteCar4() {
		return favoriteCar4;
	public void setFavoriteCar4(String[] favoriteCar4) {
		this.favoriteCar4 = favoriteCar4;
	public String getFavoriteCar1InString()
		return Arrays.toString(favoriteCar1);
	//generated by Array
	public String[] getFavoriteCar2Value()
		favoriteCar2 = new String [5];
		favoriteCar2[0] = "116";
		favoriteCar2[1] = "118";
		favoriteCar2[2] = "X1";
		favoriteCar2[3] = "Series 1 Coupe";
		favoriteCar2[4] = "120";
		return favoriteCar2;
	public String getFavoriteCar2InString()
		return Arrays.toString(favoriteCar2);
	//generated by Map
	private static Map<String, Object> car3Value;
		car3Value = new LinkedHashMap<String, Object>();
		car3Value.put("Car3 - 316", "BMW 316");
		car3Value.put("Car3 - 318", "BMW 318");
		car3Value.put("Car3 - 320", "BMW 320");
		car3Value.put("Car3 - 325", "BMW 325");
		car3Value.put("Car3 - 330", "BMW 330");
	public Map<String, Object> getFavoriteCar3Value()
		return car3Value;
	public String getFavoriteCar3InString() {
		return Arrays.toString(favoriteCar3);
	//generated by Object Array
	public static class Car
		public String carLabel;
		public String carValue;
		public Car(String carLabel, String carValue)
			this.carLabel = carLabel;
			this.carValue = carValue;
		public String getCarLabel()
			return carLabel;
		public String getCarValue()
			return carValue;
	public Car[] car4List;
	public Car[] getFavoriteCar4Value()
		car4List = new Car[5];
		car4List[0] = new Car("Car 4 - M3", "BMW M3 SMG");
		car4List[1] = new Car("Car 4 - X3", "BMW X3");
		car4List[2] = new Car("Car 4 - X5", "BMW X5");
		car4List[3] = new Car("Car 4 - X6", "BMW X6");
		car4List[4] = new Car("Car 4 - 745", "BMW 745");
		return car4List;
	public String getFavoriteCar4InString()
		return Arrays.toString(favoriteCar4);

2. Our JSF Pages

First, the welcome page, where we have a single checkbox and the four afore-mentioned different ways, which populate group checkboxes.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""    
      xmlns:f="" >
    	<h1>JSF 2.2 CheckBoxes Example</h1>
    		<h2>1. Single checkbox</h2>
    		<h:selectBooleanCheckbox value="#{user.rememberMe}" />Remember Me
    		<h2>2. Group of checkboxes</h2>
    		1. Hardcoded using the tag "f:selectItem" :
    		<h:selectManyCheckbox value="#{user.favoriteCar1}">
    			<f:selectItem itemLabel="Car1 - E10" itemValue="BMW E10" />
    			<f:selectItem itemLabel="Car1 - E36" itemValue="BMW E36" />
    			<f:selectItem itemLabel="Car1 - E46" itemValue="BMW E46" />
    			<f:selectItem itemLabel="Car1 - E87" itemValue="BMW E87" />
    			<f:selectItem itemLabel="Car1 - E92" itemValue="BMW E92" />	
    		2. Generated by Array :
    		<h:selectManyCheckbox value="#{user.favoriteCar2}">
    			<f:selectItems value="#{user.favoriteCar2Value}" />
    		3. Generated by Map :
    		<h:selectManyCheckbox value="#{user.favoriteCar3}">
    			<f:selectItems value="#{user.favoriteCar3Value}" />
    		4. Generated by Object, displayed using var
    		<h:selectManyCheckbox value="#{user.favoriteCar4}">
	   			<f:selectItems value="#{user.favoriteCar4Value}" var="last"
	   			itemLabel="#{last.carLabel}" itemValue="#{last.carValue}" />
	   		<h:commandButton value="Submit" action="results" />
	   		<h:commandButton value="Reset" type="reset" />

Then, just to ensure that every submitted value saved correctly, we ‘ll try to access the related getters through a JSF page:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""    
    	<h1>JSF 2.2 CheckBoxes Example - Response Page</h1>
    		<li>user.rememberMe : #{user.rememberMe}</li>
    		<li>user.favoriteCar1 : #{user.favoriteCar1InString}</li>
    		<li>user.favoriteCar2 : #{user.favoriteCar2InString}</li>
    		<li>user.favoriteCar3 : #{user.favoriteCar3InString}</li>
    		<li>user.favoriteCar4 : #{user.favoriteCar4InString}</li>

3. Demo

I ‘ll just select my favorites from each group:


Let’s see what happened:


This was an example of CheckBoxes in JSF 2.0. You can also download the source code for this example: CheckBoxesJSF

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books