how to create table for any java type

Imagine that your have a lot of tables. Millions.. well... tens... Still its boring to create table after table, page after page. Why not thing about something generic, dynamic?
We all know 'dataTable' component. Its about show some table with some data. Most programmers found the 'h:dataTable' component hard to customize, difficult to understand and to maintain. In this case almost impossible to create 'generic' table.
Lets see how to overcame this problem, using simple loop in loop and reflection!

New to Java, JSF, Icefaces? Here is some links to start with:
Why and when use ICEFACES?
First steps with jsf and java

We use simplest HTML tags, together with "ui:repeat" tag, which loops on 'tr' element. Then loop on 'column' inside 'tr' element.
Customization is simple too. Style of 'td' element can be dynamic.
Use you favorite HTML editor, or JSF editor together with CSS tools to customize every aspect of this table, its easy!
fragment from TableForAnyJavaType.jspx
		<caption>table for any java type</caption> 
			<ui:repeat value="#{tableForAnyJavaTypeBean.columnsTitles}" 
				<td><h:outputText value="#{title}"></h:outputText></td> 
		<!-- using double loop, first on rows, then on cells --> 
		<ui:repeat value="#{tableForAnyJavaTypeBean.dynamicData}" var="dynamicRow"> 
				<ui:repeat value="#{dynamicRow}" var="cell"> 
					<td class="#{}"><h:outputText value="#{cell.value}"></h:outputText></td> 

This is main entry point. Its referenced by 'tableForAnyJavaTypeBean' from faces-config.xml In this case we use simple reflection to collect data from getters methods. The java type of 'row' is a parameters and can set in constructor or via setter.
package com.gpost.jsfexamples.tableForAnyJavaType;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.gpost.jsfexamples.tableWithLoop.ManWithPhone;

* Java class for example 'table for any java type'
* referenced as 'tableForAnyJavaTypeBean' from faces-config.xml, 
* used in 'TableForAnyJavaType.jspx'
public class TableForAnyJavaTypeBean{
	// javaType of table row
	Class javaType;
	List<Object> rows = null;
	List<String> columnsTitles = null;

	// dynamic data
	List<List<Cell>> dynamicData = null;
	public TableForAnyJavaTypeBean() {
		// for example only use this as default
	public TableForAnyJavaTypeBean(Class javaType) {
		this.javaType = javaType;
		// find out names of the getters and use them as titles
		columnsTitles = new ArrayList<String>();
		Method []methods = javaType.getDeclaredMethods();
		for (Method method : methods) {
			if (method.getName().startsWith("get")) {
				// cut 'get'
				String title = method.getName().substring(3);
				// the order of title is defined from java type.
				// use annotation to define real order if you need,
				// or sort title by any other criteria

	public List<Object> getRows() {
		if (rows == null) {
			rows = new ArrayList();
			// create same sample data...
			rows.add(new ManWithPhone("Gary", "1234-6767"));
			rows.add(new ManWithPhone("Bob", "5678-4553"));
		return rows;
	public List<String> getColumnsTitles() {
		return columnsTitles;

	 * return double list of cells. exception handling can be improved.
	public List<List<Cell>> getDynamicData()  {
		if (dynamicData == null) {
			dynamicData = new ArrayList<List<Cell>>();
			for (Object someObject : getRows()) {
				List<Cell> cells = new ArrayList<Cell>();
				// get value in the same order as defined in 'columnsTitles' variable
				for (String title : columnsTitles) {
					Method getter;
					try {
						// here we use reflection
						getter = someObject.getClass().getMethod("get"+title, null);
						Object value = getter.invoke(someObject, null);
						Cell cell = new Cell();
					} catch (SecurityException e) {
					} catch (NoSuchMethodException e) {
					} catch (IllegalArgumentException e) {
					} catch (IllegalAccessException e) {
					} catch (InvocationTargetException e) {
		return dynamicData;

	public Class getJavaType() {
		return javaType;

	public void setJavaType(Class javaType) {
		this.javaType = javaType;

} // end of class

This class defines data in row.
package com.gpost.jsfexamples.tableForAnyJavaType;

public class ManWithPhone {
	public ManWithPhone(String name, String phone) {
		super(); = name; = phone;

	String name;
	String phone;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) { = phone;

This class defines data in row.
package com.gpost.jsfexamples.tableForAnyJavaType;

 * contains value of cell and style
public class Cell {
	String value;
	String style;
	public String getValue() {
		return value;
	public void setValue(String value) {
		this.value = value;
	public String getStyle() {
		return style;
	public void setStyle(String style) { = style;

part of faces-config.xml


As we saw in this example, you can create perfect table for any java type using simple html/css, then add some binding to your java beans and you get very flexible, easily customizable data table without complexity of 'h:dataTable' component. Every aspect of table-tr-td combination can be dynamic, controlled by your business logic or in every other way.

No comments:

Post a Comment