JasperReports provides several types of pre-defined/ready-to-use datasources which can be used to provide the data that is filled in the report. We are going to look at one such datasource, the ‘JavaBeans Set Datasource’, which allows us to use JavaBeans as data to fill reports.

The basic idea is to provide 2 important things –

1. A JavaBean Class which defines the different fields of the datasource
2. A bean factory class which essentially returns a set of objects of the JavaBean class in form of an Array or Collection. Each object inside the array or the collection will be seen as one record in this type of data source.

We will now discuss each of these classes and how are they used in the datasource with an example.

1. Creating the Java Bean

The JavaBean Class is a Java class which exposes its attributes using “getter” methods. We define a “getter” method for every attribute which looks like public getXXX() , where XXX is the attribute in the class.
The example below, shows a WeatherBean Class which holds weather information for various cities. You can see a “getter” (accessor) method defined for each of the attributes as per the specified format.

* Bean Class which defines that data structure and provides the getter methods 
public class WeatherBean {
	private String cityName = ""; 
	private String weatherType = "";
	private int temperature = 0;

	public WeatherBean(String cityName, String weatherType, int temperature) {
	this.cityName = cityName;
	this.weatherType = weatherType;
	this.temperature = temperature;

	public String getcityName() {
	return cityName;

	public String getweatherType() {
	return weatherType;

	public int gettemperature() {
	return temperature;

2. Creating the bean factory class

The bean factory class should have a static method which will return a collection or an array of objects of the bean class. In the below example the method is getWeatherBeans().

import java.util.Collection;
import java.util.Vector;

* Bean Factory Class which returns a collection of beans
public class WeatherBeanFactory {

public static Collection getWeatherBeans(){
Vector allbeans = new Vector();
try {
allbeans.add(new WeatherBean("Delhi","Sunny",41));
allbeans.add(new WeatherBean("Mumbai","Cloudy",36));
allbeans.add(new WeatherBean("Bangalore","Rainy",28));
allbeans.add(new WeatherBean("Hyderabad","Sunny",39));
catch(Exception e){
return allbeans;

Export both the classes into a JAR file.

3. Add the JAR file to the iReport Classpath


4. Create the JavaBean Set Datasource

Select the datasource type as “JavaBeans set datasource”


Set the Factory class name and Static method call to retrieve the collection:
In our example, it is “WeatherBeanFactory” and “getWeatherBeans”


5. Set the Report Query
Go to the “JavaBean” datasource tab. In class name, give the name of the bean class. In our example, its “WeatherBean”. Click “Read attributes” and select the fields as per your requirement.


The mapping between a particular JavaBean property/attribute and the corresponding report field is made by naming conventions. The name of the report field must be the same as the name of the JavaBean property as specified by the JavaBeans specifications. For instance, to retrieve the value of a report field named cityName, the program will try to call through reflection a method called getcityName() on the current JavaBean object. This is how, it “getter” methods are used.
Below report is designed using the fields from the “WeatherBean” class.


The output is shown below.


Though we have used a simple JavaBean class, with attributes as basic types, Nested JavaBean properties can be also accessed in a JavaBean data source.

– Shraddha
Helical IT Solutions

Leave a Reply