JSF 2 PanelGrid and PanelGroup Example

The components <h:panelGrid/> & <h:panelGroup/> are mainly used as an alternative option for laying out the components into a certain form by generating the HTML markup for laying out components in rows and columns. At the other side, <h:panelGrid/> is often used in conjunction with <h:panelGroup/>, which groups two or more components so they are treated as one.

The most common use for panel group Tag is when it comes to group an input component as an example associated with its error message (Will be discussed later). Grouping the text field and error message puts them in the same table cell. Both of those components are provided a different attributes for styling the different part of their body, in case of using <h:panelGrid/> you’ve the ability to use a wide range of styling attributes such as bgcolor, columnClasses, footerClasses, headerClasses, rowClasses, captionClass and captionStyle beside those basic HTML 4 attributes – A panel caption is an optionally supplied by the facet named (caption) – but in case of using <h:panelGroup/> you’ve ability to use the basic HTML 4 attributes such as style and styleClass.

Also Read:

1. Managed Bean


package net.javabeat.jsf;

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

public class IndexBean {
	private String outcome = "index";
	private String userInput = "";

	public String getOutcome() {
		return outcome;

	public void setOutcome(String outcome) {
		this.outcome = outcome;

	public String getUserInput() {
		return userInput;

	public void setUserInput(String userInput) {
		this.userInput = userInput;

	public String addFacesMessage(){
		// Add a message for the input
				new FacesMessage(FacesMessage.SEVERITY_ERROR, "Say Hello JavaBeat !", "Say Hello JavaBeat !"));
		// This empty string token will be understood by the JSF navigation handler
		// This empty token means, don't take me off this view
		return "";

2. The View


<html xmlns="http://www.w3.org/1999/xhtml"
	<title>JavaBeat JSF2 Button &amp; Link Components</title>
		.col {
			background-color: red;
		.row {
			font-style: italic;
		.caption {
			background-color: aqua;
			font-style: inherit;
			font-weight: bolder;
		.header {
			background-color: yellow;
			font-style: oblique;
	<h:form prependId="false">
			<h:outputText value="JavaBeat JSF 2.2 Examples" />
			<h:outputText value="PanelGrid &amp; PanelGroup Example" />
		<h1>Panel Grid Demo:</h1>
		<h:panelGrid columns="2" border="1" columnClasses="col,col"
							headerClass="header" captionClass="caption"
			<f:facet name="header">
				<h:outputText value="Header"/>
			<f:facet name="caption">
				<h:outputText value="Caption"/>
			<h:outputText value="JavaBeat Component First Row/First Column"/>
			<h:outputText value="JavaBeat Component First Row/Second Column"/>
			<h:outputText value="JavaBeat Component Second Row/First Column"/>
			<h:outputText value="JavaBeat Component Second Row/Second Column"/>
			<h:outputText value="JavaBeat Component Third Row/First Column"/>
			<h:outputText value="JavaBeat Component Third Row/Second Column"/>

		<h1>Panel Group Demo:</h1>
		<h:panelGroup id="panelGroup" columns="2">
			<h:inputText id="input" value="#{indexBean.userInput}"/>
			<h:commandButton value="Send A Message For JavaBeat !" action="#{indexBean.addFacesMessage}"/>
			<h:message for="input" showDetail="true" showSummary="false" errorStyle="color:red;"/>

		<h1>Panel Group Inside PanelGrid Demo:</h1>

		<h:panelGrid columns="1" columnClasses="col,col">
			<h:panelGroup columns="2">
				<h:outputText value="JavaBeat Component First Row/First Column "/>
				<h:outputText value="JavaBeat Component First Row/Second Column "/>
			<h:panelGroup columns="2">
				<h:outputText value="JavaBeat Component Second Row/First Column "/>
				<h:outputText value="JavaBeat Component Second Row/Second Column "/>

3. The Deployment Descriptor (web.xml)


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5" metadata-complete="true">
		<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>

4. JSF 2 PanelGrid & PanelGroup Demo

The below snapshot shows you the complete scenario of using the panelGrid and panelGroup.
JSF 2 PanelGrid & PanelGroup Example
Note the following important points that shown in the above snapshot:

  • The panel grid component used for laying out the components.
  • The panel group component used for grouping set of components and make them one component. This grouping is very clear if you’ve noted that at the (Panel Group Inside Panel Grid Demo). Using of panel group components does affect the panel grid laying procedure; so rather than displaying the 4 rows cause the panel grid have been configured to accept 1 column per row, it does render a panel group per each row and it discard the contents of the panel group component, although each panel group have contained more than one column.
  • The panel group does treat the input and its message as a one component, so they are laying on the same cell (See Panel Group Demo).

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!