Programming Tutorials

Using Expressions in JasperReports

By: MakeItSimple. in Java Tutorials on 2008-09-02  

Expressions are a powerful feature of JasperReports. They can be used for declaring report variables that perform various calculations, for data grouping on the report, to specify report text fields content or to further customize the appearance of objects on the report.

Basically, all report expressions are Java expressions that can reference report fields and report variables.

In an XML report design there are several elements that define expressions: 

<variableExpression>,
<initialValueExpression>,
<groupExpression>,
<printWhenExpression>,
<imageExpression> and <textFieldExpression>
.

In order to use a report field reference in an expression, the name of the field must be put between $F{ and } character sequences.  

For example, if we want to display in a text field, on the report, the concatenated values of two fields, we can define an expression like this one:  

<textFieldExpression>  $F{FirstName} + " " + $F{LastName}</textFieldExpression>

The expression can be even more complex:

<textFieldExpression>  $F{FirstName} + " " + $F{LastName} + " was hired on " +   (new SimpleDateFormat("MM/dd/yyyy")).format($F{HireDate}) + "."</textFieldExpression>

To reference a variable in an expression, we must put the name of the variable between $V{ and } like in the example below:

<textFieldExpression>  "Total quantity : " + $V{QuantitySum} + " kg."</textFieldExpression>

There is an equivalent syntax for using parameters in expressions. The name of the parameter should be put between $P{ and } like in the following example:

<textFieldExpression>  "Max Order ID is : " + $P{MaxOrderID}</textFieldExpression>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)