Skip to content

Jasper Reports

JasperReports tips: internationalization, pagination, split types and floating elements.

2 min read

References

Internationalization

There are two ways to associate the java.util.ResourceBundle object with the report template.

  • At design time, by setting the resourceBundle attribute of the report template object to the base name of the target resource bundle.
  • A dynamic/runtime association can be made by supplying a java.util.ResourceBundle object as the value for the REPORT_RESOURCE_BUNDLE parameter at report-filling time.

Example (at design time):

Resources.properties or Resources_en_GB.properties

localization.home=Home
<jasperReport
...
resourceBundle="Resources"
...
<textField>
<reportElement x="300" y="20" width="50" height="15" />
<textElement textAlignment="Center" />
<textFieldExpression class="java.lang.String"><![CDATA[$R{localization.home}]]></textFieldExpression>
</textField>

In order to internationalization of parameter $P or dataset field $F, you can use the method str().

<textField>
<reportElement x="300" y="20" width="50" height="15" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[str($F{fieldName})]]></textFieldExpression>
</textField>

If the report needs to be generated in a locale that is different from the current one, the built-in REPORT_LOCALE parameter can be used to specify the runtime locale when filling the report.

Map<String, Object> parameters = new HashMap();
parameters.put(JRParameter.REPORT_LOCALE, new Locale("en", "GB"));

Pagination

The common approach, as you mentioned, uses two separated text fields:

Current page number: $V{PAGE_NUMBER} with EvaluationTime: Now

Total page number: $V{PAGE_NUMBER} with EvaluationTime: Report

<textField>
<reportElement x="130" y="130" width="120" height="20"/>
<textFieldExpression><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="250" y="130" width="115" height="20"/>
<textFieldExpression><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
</textField>

For JasperReports 6+, use MASTER_CURRENT_PAGE and MASTER_TOTAL_PAGES system variables and remember to set the text field evaluationTime to Master:

<textField evaluationTime="Master">
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression>
</textField>

New page, splitType

Create a new band with splitType="Immediate" property to add a new page.

<band height="600" splitType="Immediate">

Floating, positionType

If you need to have a staticText to float below another element with variable height, you can use positionType="Float".

Floating object will move down if necessary, but if the previous object is smaller than expected, it doesn’t move up.

According to the documentation, the floating element would have its y attribute ignored.

<staticText>
<reportElement positionType="Float" x="0" y="190" width="495" height="15" />
<textElement verticalAlignment="Middle" />
<text><![CDATA[Text is here]]></text>
</staticText>

Related articles

3 min read

Jakarta

Jakarta EE namespaces, project settings and what changed in Jakarta EE 10 and 11.

  • Java & Jakarta EE
8 min read

JSF

Jakarta Faces (JSF) notes: WebSocket push, scopes, events, Ajax, security and cross-field validation.

  • Java & Jakarta EE
  • Web & Frontend
2 min read

JDBC

JDBC connection pools, driver and DataSource classes per database, and local versus XA transactions.

  • Java & Jakarta EE
  • Databases

Type to search the whole site.