A report title can be static or dynamic. A static title is fixed at design time, that is while designing the report. It remains unchanged and, therefore, every time you generate the report, it will have the same title.
An example of a fixed title would be "Monthly Customer Invoices" for a report that shows all invoices for a specific customer in a month.
In many cases, you may need every copy of your report to have some copy-specific words in its title. For example, if you are generating a copy of the "Monthly Customer Invoices" report, you may like to display the name of the customer in the title. This will make your title dynamic.
In this recipe, I will show you how to generate dynamic titles, which have some words that are not fixed while you are designing the report. JasperReports will decide these copy-specific words every time you generate a copy of your report.
In order to work with this recipe, you need to have a relational database installed on your computer. I prefer a simple and easy-to-use open source database, which makes a small footprint on your PC. That's because I am providing sample data with this recipe, so I would like to make it as easy and quick as possible for readers to import the sample data into their databases before executing the steps of this recipe.
I have used the PostgreSQL database, which fulfills these criteria. You can visit the web site (http://www.postgresql.org/) and get all the information to install and start using PostgreSQL. You may also refer to the installPostgreSQL.txt
file included in the source code download for this chapter, which shows how you will install and run PostgreSQL. Make sure that PostgreSQL is up and running before you proceed.
The source code for this chapter also includes a copySampleDataIntoPGS.txt
file, which will help you create a table named CustomerInvoices
with five columns ( InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data for all the recipes of this chapter into the table.
After inserting the required data, you will connect iReport to your database hosted on PostgreSQL using the steps described in the Creating a report from relational data recipe later in Chapter 4.
DynamicTitle.jrxml
file from the Task4
folder in the source code for this chapter. The Designer tab of iReport shows an empty report with a simple main title (Monthly Customer Invoices) as shown here: Text Field
from the palette of components available on the right of the main iReport window and drop it into the Title section of your report, just below the main title. Add Parameter
from the pop-up menu. parameter1
, which appears at the end of the list of parameters. Select parameter1
from the expanded view. You will see the properties of this parameter in the Properties window on the right of the Designer tab just below the palette of components. Name
property of the parameter and type CustomerName
as its value. This sets CustomerName
as the name of this parameter. Similarly, click on the Default Value Expression property of the parameter and type""
(two double quotes) as its value. Leave the rest of the properties at their default values. CustomerName
parameter. For this purpose, right-click on the text field and select the Edit expression option from the pop-up menu. An Expression editor window will open as shown below:($F{field})
from the Expression editor window. CustomerName
parameter. Double-click on it. This will set $P{CustomerName}
in the expression editor, as shown below: CustomerName
parameter with your text field of step 2.SELECT * FROM "public"."CustomerInvoices" WHERE "public"."CustomerInvoices"."CustomerName" = $P{CustomerName}
CustomerInvoices
table (that is, InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and show them in the lower part of the Report query window, as shown next: Packt Publishing
as the name of the customer and click OK. You will see a dynamic title, as shown next.Open the XML tab of your report and you will see the following JRXML code:
<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <parameter name="CustomerName" class="java.lang.String"> </parameter> <queryString> <![CDATA[SELECT * from "public"."CustomerInvoices" WHERE "public"."CustomerInvoices"."CustomerName" = $P{CustomerName}]]> </queryString> <!-- other JasperReports XML tags --> <title> <band height="79" splitType="Stretch"> <staticText> <!-- other JasperReports XML tags --> <text><![CDATA[Monthly Customer Invoices]]></text> </staticText> <textField> <reportElement x="113" y="49" width="128" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"> <![CDATA[$P{CustomerName}]]> </textFieldExpression> </textField> <!-- other static text and text field XML tags --> </band> </title> <!-- other JasperReports XML tags --> </jasperReport>
You can see that this JRXML contains<parameter>, <queryString>
, and<title>
tags in expanded view, whereas all other tags that are not relevant to the present discussion of the recipe are omitted.
iReport has generated the<parameter>
tag (shown highlighted in the code) in response to steps 3 and 4 of the recipe. In these steps, you configured the CustomerName
parameter. The<parameter>
tag wraps the name of the parameter (CustomerName
) and the type of data (that is, a Java string) through its name
and class
attributes, respectively.
Now look at the<queryString>
tag (shown highlighted in the code), which wraps the SQL query you authored in step 11 of the recipe to fetch table data from your database.
Also notice from the SQL query that it contains a string $P{CustomerName}
on the right of the =
sign in the Where
clause. This $P
is not SQL syntax. This $P
will be internally processed by the JasperReports engine, which resolves it to the CustomerName
parameter before executing the query.
This way, the SQL query becomes part of your JRXML code and the JasperReports engine will process and execute this query to fetch the customer name from your database. JasperReports will also copy the name of the customer into the CustomerName
parameter while processing your report.
Now look at the<staticText>
tag whose<textFieldExpression>
child is shown highlighted in the code. This<textFieldExpression>
tag specifies the parameter (CustomerName
) that contains the name of the customer fetched from the database. Therefore, when JasperReports processes this<staticText>
tag, it will show the name of the customer in the title of your report.
Use the overflow
attribute. At runtime, if the title width exceeds the given width, it won't appear.
Also, it is better to externalize the static text into PROPERTIES files so that it will be easy to support multilingual support down the line. Also, it will be easy to fix spelling mistakes or modifications, without touching the report.
18.218.97.75