If-else in Jasper Reports expressions


Jasper Reports do not support if-else statements when defining variable expressions. Instead it relies on the ternary operators {cond} ? {statement 1} : {statement 2} which means you have to nest the operators if your expression depends on more than one condition. This blog demonstrates how you can (somewhat) get around this by implementing your own class to handle multiple if-else conditions and use it in Jasper Reports.

If-else in Jasper Reports

Suppose you want to define a text expression to render different text based on the value of a field or variable. In Jasper Reports, you have to nest the ternary operator as follows:

$F{cond}.equalsIgnoreCase("NSW") ? "Go Sydney!" :
( $F{cond}.equalsIgnoreCase("QLD") ? "Go Brisbane!" :
    ($F{cond}.equalsIgnoreCase("VIC") ? "Go Melbourne!" :
      "Go Aussie!")

As the number of conditions increase, you will have to nest the ternary operators which will make the expression harder to read if you have more than 4 or more conditions to test.

Extending Jasper Reports for multiple if-elses

The solution described here involves implementing a new class to emulate if-else statements in Java which can then be used in defining expressions in Jasper Reports. Instead of using nested ternary operators illustrated in previous section, the expression will look like:

new JasperIf().
jif($F{cond}.equalsIgnoreCase("NSW")).jthen("Go Sydney!")
.jelseif($F{cond}.equalsIgnoreCase("QLD"), "Go Brisbane!")
.jelseif($F{cond}.equalsIgnoreCase("VIC"), "Go Melbourne!")
.jelse("Go Aussie!")

1. Implement the JasperIf class

I include the java class I implemented below. The codes are fairly self explanatory. Note the implementations of methods jif, jelseif, jelse to emulate the corresponding Java keywords.

package com.rlee.jasperext;

import java.util.ArrayList;
import java.util.List;

public class JasperIf {

    private Boolean ifCondition;
    private List<ElseIf> elseifConditions;
    private String thenStatement;
    private String elseStatement;

    public JasperIf() {
        this.elseifConditions = new ArrayList<ElseIf>();

    public JasperIf jif(Boolean ifCond) {
        this.ifCondition = ifCond;
        return this;

    public JasperIf jthen(String then) {
        this.thenStatement = then;
        return this;

    public JasperIf jelse(String elseStatement) {
        this.elseStatement = elseStatement;
        return this;

    public JasperIf jelseif(Boolean elseif, String elseifStatement) {
        this.elseifConditions.add(new ElseIf(elseif, elseifStatement));
        return this;

    public String toString() {
        return evaluate();

    private String evaluate() {
        if (ifCondition) {
            return thenStatement;
        } else {
            for (ElseIf elseif : elseifConditions) {
                if (elseif.cond) {
                    return elseif.statement;
        return elseStatement;

    private class ElseIf {
        private Boolean cond;
        private String statement;

        ElseIf(Boolean cond, String statement) {
            this.cond = cond;
            this.statement = statement;

2. Add class to iReport

To use the above class in your Jasper Reports template, you will first have to package it in a jar file and then add the jar file in your iReport installation. You do this by adding the jar file to directory <Your JasperReport dir>/ireport/modules/ext. For example, C:\Program Files (x86)\Jaspersoft\iReport-4.6.0\ireport\modules\ext.

In iReport,

  1. Go to menu Tools->Options
  2. Go to Classpath tab under iReport icon, add the path to jar file (see screenshot below)

3. Restart iReport

3. Use JasperIf class in report templates

Now you can use the new class in the report’s expression. For example,

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" 
    <import value="com.rlee.jasperext.JasperIf"/>
        <variableExpression><![CDATA[new JasperIf().
jif($F{cond}.equalsIgnoreCase("NSW")).jthen("Go Sydney!")
.jelseif($F{cond}.equalsIgnoreCase("QLD"), "Go Brisbane!")
.jelseif($F{cond}.equalsIgnoreCase("VIC"), "Go Melbourne!")
.jelse("Go Aussie!")]]></variableExpression>

That’s it. The solution is a bit of work around to avoid deep nesting of ternary operators for expressions that require multiple conditions check. It’s still a bit wordy. Note also the same approach can be used to extend Jasper Reports if you have complex rendering logic based on data values in the reports.

About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.