Almost every report has a footer. The simplest footer will provide information about the report such as the name of the report and the date and time when the report was generated among others.
This recipe teaches you how you will display the name of the report and the date when the report was generated.
The steps to a simple footer design are as follows:
ASimpleFooter.jrxml
file from the Task6
folder of the source code for this chapter. The Designer tab of iReport shows that the report contains a main title, a subtitle, a page header, a column header, and an empty footer, as shown in the following screenshot:($F{field})
from the Expression editor window. JASPER_REPORT
parameter and select it. This will list all methods for this parameter in the adjacent third column. Scroll down the list of methods in the third column to find the getName()
method. Double-click on it. This will set $P{JASPER_REPORT}.getName()
in the Expression editor, as shown in the following screenshot: $P{JASPER_REPORT}.getName()
expression to your text field component of step 2. new java.util.Date()
expression will appear in the Page Footer section of your report. Select this date text field component and use the arrow keys to roughly align this text field horizontally and vertically to the right-middle position in the Page Footer section of your report, as shown in the following screenshot: designed by PACKT
text in it. Then select the static text component and use the arrow keys to select its position, as shown in the following screenshot: by PACKT)
by pressing Ctrl+C. Then double-click on the text field component with the expression $P{JASPER_REPORT}.getName()
,. The text of expression will be selected. Deselect the text and move your cursor to the end of the expression. Type +"
and then a space, now press Ctrl+V to insert your copied static text (designed by PACKT)
. The expression in the text field component will change to $P{JASPER_REPORT}.getName() +" designed by PACKT
. Move your cursor to the end of expression and press the " key of your keyboard. Now your expression in the text field component will look like $P{JASPER_REPORT}.getName() +" designed by PACKT"
. $P{JASPER_REPORT}.getName() +" designed by PACKT"
using your mouse and increase its size by dragging its corners to show a full text view, as shown in the following screenshot:You have learned how to display the name of your report and the date when the report was generated in the footer. You have also learned the small trick of combining two components into one in step 17 of the recipe. Switch to the XML tab to see how all this works:
<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <!-- other JasperReports XML tags --> <pageFooter> <band height="32" splitType="Stretch"> <textField> <reportElement x="25" y="7" width="279" height="20" isPrintInFirstWholeBand="true"/> <textElement/> <textFieldExpression class="java.lang.String"> <![CDATA[$P{JASPER_REPORT}.getName() +" designed by PACKT"]]> </textFieldExpression> </textField> <textField pattern="MMMMM dd, yyyy"> <reportElement x="446" y="7" width="100" height="20"/> <textElement/> <textFieldExpression class="java.util.Date"> <![CDATA[new java.util.Date()]]> </textFieldExpression> </textField> </band> </pageFooter> <!-- other JasperReports XML tags --> </jasperReport>
I have shown only a<pageFooter>
tag (which corresponds to the Page Footer section of your report) omitting all the other tags, which are not relevant to this discussion.
The<pageFooter>
tag has a<band>
child, with two children, both named<textField>
. The first<textField>
tag corresponds to the name of the report that you dropped in step 2 of the recipe and later edited in step 17. Notice that the expression and text combination that you authored in step 17 ($P{JASPER_REPORT}.getName() +" designed by PACKT)
is part of the content inside the first<textField>
tag. This means when you followed step 17, iReport copied the expression and text combination into the first<textField>
tag.
The second<textField>
tag simply corresponds to the Current date component that you dropped in step 9 of the recipe.
Also look at the $P{JASPER_REPORT}.getName()
expression of step 5. JasperReports provides many other useful parameters like this JASPER_REPORT
parameter, which holds information about your report (for example, the name of the report, the SQL query used in your report, and so on). You can follow steps 3, 4, and 5 to play with different available parameters and their methods to discover the different bits of information you can display in any text field.
52.15.38.176