Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
JasperReports 3.5 for Java Developers

You're reading from  JasperReports 3.5 for Java Developers

Product type Book
Published in Aug 2009
Publisher Packt
ISBN-13 9781847198082
Pages 368 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

JasperReports 3.5 for Java Developers
Credits
About the Author
About the Reviewers
Preface
1. An Overview of JasperReports 2. Adding Reporting Capabilities to our Java Applications 3. Creating your First Report 4. Creating Dynamic Reports from Databases 5. Working with Other Datasources 6. Report Layout and Design 7. Adding Charts and Graphics to Reports 8. Other JasperReports Features 9. Exporting to Other Formats 10. Graphical Report Design with iReport 11. Integrating JasperReportswith Other Frameworks Index

Chapter 8. Other JasperReports Features

JasperReports has several features that allow us to create elaborate reports. In this chapter, we will discuss some of these features.

Some of the features we will cover in this chapter include:

  • How to display report text in different languages by using report localization/internationalization

  • How to execute snippets of Java code by using scriptlets

  • How to create crosstab (cross-tabulation) reports

  • How to use subdatasets to run a query with the results of a different query

  • How to add anchors, hyperlinks, and bookmarks to the reports in order to ease navigation between report sections

Report localization


JasperReports takes advantage of the Java language's internationalization features to be able to generate reports in different languages. The following JRXML template will generate a report displaying a line of text that will be different depending on the locale used:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jasperreports.sourceforge.net            /jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
              name="LocalizationDemoReport" 
              resourceBundle="localizationdemo">
  <summary>
    <band height="60">
      <textField>
        <reportElement x="0" y="0" width="200" height="30" />
        <textFieldExpression>
          <![CDATA[$R{localization.text1}]]>
        </textFieldExpression>
      </textField>...

Scriptlets


JasperReports allows us to execute snippets of Java code at certain points during the report filling process. We can accomplish this by writing scriptlets. All the scriptlets must extend either net.sf.jasperreports.engine.JRAbstractScriptlet ornet.sf.jasperreports.engine.JRDefaultScriptlet. Following is a brief explanation on these classes:

  • JRAbstractScriptlet: This contains a number of abstract methods that must be overridden in every implementation. These methods are called automatically by JasperReports at the appropriate moment.

  • JRDefaultScriptlet: This is a convenience class containing default empty implementations of every method in JRAbstractScriptlet. It can be used whenever we wish to override only a few of the methods in JRAbstractScriptlet.

The following table summarizes these methods:

Crosstabs


Crosstabs (cross-tabulation) reports are the reports containing tables that tabulate the data across rows and columns. This feature was introduced in JasperReports 1.1. The following example illustrates the use of crosstabs in a report. The JRXML template will generate a report displaying a table containing the number of aircraft in each city of the state of New York. The last column of the table will display the total number of aircraft for all models in each city. The last row will display the total number of aircraft of each model in the table. To avoid having an unmanageable number of columns in the table, we will limit the report to aircraft models that start with the letter "C".

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport...

Subdatasets


Sometimes we would like to display related charts or crosstabs for similar data grouped differently. For example, in the previous section, we generated a crosstab displaying the total number of aircraft of a particular set of models in the state of New York. We can display the same set of data for different states by using subdatasets. The following example illustrates how to do this:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation= "http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
  name="DatasetDemoReport" leftMargin="5" rightMargin="5">
  <subDataset name="Aircraft_Models">
    <parameter name="StateParam" class="java.lang.String" />
    <queryString>
      <![CDATA[select a.city, am.model, a.tail_num
               from aircraft...

Adding hyperlinks and anchors to reports


JasperReports allows us to add hyperlinks and anchors to our reports. The only report elements that can be hyperlinks or anchors are text fields, charts, and images. Hyperlinks allow us to quickly navigate between different report sections, a feature that is very useful when producing long reports. The following example illustrates how to add hyperlinks to our reports:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
         name="HyperLinkDemoReport" leftMargin="5" rightMargin="5">
  <title>
    <band height="60">
      <staticText>
        <reportElement x="0" y="0" width="555" height="30" />
          <text>
            <![CDATA[In a rush...

Handling very large reports


Sometimes, when filling a report, the report datasource may have a lot of data. In some cases, the generated report can become very large, and in some cases larger than the memory allocated for the JVM, causing an OutOfMemoryException.

It is possible to set up JasperReports so that it stores segments of a report on the disk in order to free some memory. This can be accomplished by using a built-in report parameter REPORT_VIRTUALIZER. The value for this parameter must be an instance of a class implementing net.sf.jasperreports.engine.JRVirtualizer. JasperReports comes with an implementation of this interface, namely net.sf.jasperreports.engine.fill.JRFileVirtualizer. This implementation is sufficient to handle the vast majority of the large reports. If, for some reason, this implementation is not sufficient for our needs, we can always create our own implementation of net.sf.jasperreports.engine.JRVirtualizer. The following example illustrates typical usage of JRVirtualizer...

Summary


In this chapter, we discussed several features that allow us to create elaborate reports. We learned to render localized reports by using the resourceBundle attribute of the <jasperReport> JRXML element. We then used scriptlets to add complex functionality to our reports, including variable value modification and performance measurement. We saw how to add cross-tabulation tables (crosstabs) to our reports by taking advantage of the <crosstab> JRXML element and display related charts or crosstabs for each record in a report by using subdatasets. To ease the task of report navigation, we learned how to add hyperlinks, anchors, and bookmarks to our reports. We have also seen how we can safely generate reports larger than the available memory by taking advantage of report virtualization.

lock icon The rest of the chapter is locked
You have been reading a chapter from
JasperReports 3.5 for Java Developers
Published in: Aug 2009 Publisher: Packt ISBN-13: 9781847198082
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}

Method

Description

public void beforeReportInit()

Called before report initialization.

public void afterReportInit()

Called after report initialization.

public void beforePageInit...