ZetCode

JasperReports variable

last modified February 12, 2024

JasperReports variable tutorial shows how to create a variable in JasperReports library.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented, ready-to-print documents.

JasperReports variable

A variable is used to store partial results and to do complex calculations with the data extracted from data source. They can be used in other parts of the report, including other variables. We refer to the variable with the $V{var_name} syntax.

A variable is defined with the <variable> tag. The expression to calculate the variable is defined in the <variableExpression> tag. Inside the expression, we can use functions, operators, fields, or other variables. JasperReports contains several built-in variables such as PAGE_NUMBER, COLUMN_NUMER, or REPORT_COUNT.

JasperReports variable example

The following application creates a sum variable, which sums the price of cars.

pom.xml
src
└───main
    ├───java
    │   └───com
    │       └───zetcode
    │           ├───main
    │           │       CommandLineRunner.java
    │           │       JasperVariableSum.java
    │           └───model
    │                   Car.java
    └───resources
            report2.xml

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>sumex</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <exec.mainClass>com.zetcode.main.CommandLineRunner</exec.mainClass>
    </properties>

    <dependencies>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.9.0</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.9.0</version>
        </dependency>

    </dependencies>

</project>

The Maven pom.xml file contains the jasperreports and the jasperreports-fonts dependencies. The fonts are needed because we display the sum in bold.

com/zetcode/model/Car.java
package com.zetcode.model;

public class Car {

    private Long id;
    private String name;
    private int price;

    public Car() {}

    public Car(Long id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Car{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", price=").append(price);
        sb.append('}');
        return sb.toString();
    }
}

This is Car bean class. It contains car id, car name, and car price attributes.

src/resources/report2.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport xmlns = "http://jasperreports.sourceforge.net/jasperreports"
              xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation = "http://jasperreports.sourceforge.net/jasperreports
                                    http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
              name="report2" topMargin="20" bottomMargin="20">

    <style name="sum" isBold="true"/>

    <field name="Price" class="java.lang.Integer">
        <fieldDescription><![CDATA[price]]></fieldDescription>
    </field>

    <variable name="sum" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{Price}]]></variableExpression>
    </variable>

    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20"/>
                <box leftPadding="10"/>
                <textElement textAlignment="Center"/>
                <text><![CDATA[Car price]]></text>
            </staticText>
        </band>
    </columnHeader>

    <detail>
        <band height="20">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" />
                <textElement textAlignment="Right"/>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{Price}]]></textFieldExpression>
            </textField>
        </band>
    </detail>

    <summary>
        <band height="20">
            <textField>
                <reportElement style="sum" x="0" y="0" width="100" height="20"/>
                <textElement textAlignment="Right"/>

                <textFieldExpression class="java.lang.Long"><![CDATA[$V{sum}]]></textFieldExpression>
            </textField>
        </band>
    </summary>

</jasperReport>

This is the report template file. The template contains the column header and the detail bands.

<style name="sum" isBold="true"/>

We define a style for the sum; it is rendered in bold font.

<field name="Price" class="java.lang.Integer">
    <fieldDescription><![CDATA[price]]></fieldDescription>
</field>

The Price field is mapped to the car's price attribute. In the example, we display only car prices.

<variable name="sum" class="java.lang.Integer" calculation="Sum">
    <variableExpression><![CDATA[$F{Price}]]></variableExpression>
</variable>

We define the sum variable. The calculation attribute defines the function applied on the variable expression. In our case, we have the $F{Price}, which returns the price field from the data source.

<summary>
    <band height="20">
        <textField>
            <reportElement style="sum" x="0" y="0" width="100" height="20"/>
            <textElement textAlignment="Right"/>

            <textFieldExpression class="java.lang.Long"><![CDATA[$V{sum}]]></textFieldExpression>
        </textField>
    </band>
</summary> 

We display the sum variable in the summary band with the $V{sum} syntax.

com/zetcode/main/JasperVariableSum.java
package com.zetcode.main;

import com.zetcode.model.Car;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.util.ArrayList;
import java.util.HashMap;

public class JasperVariableSum {

    public void start() throws JRException {

        var cars = new ArrayList<Car>();

        cars.add(new Car(1L, "Audi", 52642));
        cars.add(new Car(2L, "Mercedes", 57127));
        cars.add(new Car(3L, "Skoda", 9000));
        cars.add(new Car(4L, "Volvo", 29000));
        cars.add(new Car(5L, "Bentley", 350000));
        cars.add(new Car(6L, "Citroen", 21000));
        cars.add(new Car(7L, "Hummer", 41400));
        cars.add(new Car(8L, "Volkswagen", 21600));

        var ds = new JRBeanCollectionDataSource(cars);

        var xmlFile = "src/main/resources/report2.xml";
        var jasperReport = JasperCompileManager.compileReport(xmlFile);

        var params = new HashMap<String, Object>();
        var jasperPrint = JasperFillManager.fillReport(jasperReport,
                params, ds);

        JasperExportManager.exportReportToPdfFile(jasperPrint,
                "src/main/resources/report2.pdf");
    }
}

The data source in the example is a collection of Java beans.

com/zetcode/main/CommandLineRunner.java
package com.zetcode.main;

import net.sf.jasperreports.engine.JRException;

public class CommandLineRunner {

    public static void main(String[] args) throws JRException {

        var app = new JasperVariableSum();
        app.start();
    }
}

The CommandLineRunner sets up the application.

$ mvn exec:java

We run the application.

In this article we have created a sum variable to calculate the sum of car prices.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all JasperReports tutorials.