JasperReports 3.5 for Java developers

SHARE & COMMENT :

JasperReports 3.5 for Java developers

If you want to create easily understood, professional, and powerful reports from disordered, scattered data using a free, open source Java class library, this book on JasperReports is what you are looking for. JasperReports is the world’s most popular embeddable Java open source reporting library, providing Java developers with the power to create rich print and web reports easily.

also read:

JasperReports allows you to create better looking reports with formatting and grouping, as well as adding graphical elements to your reports. You can also export your reports to a range of different formats, including PDF and XML. Creating reports becomes easier with the iReport Designer visual designing tool. To round things off, you can integrate your reports with other Java frameworks, using Spring or Hibernate to get data for the report, and Java Server Faces or Struts for presenting the report.
This book shows you how to get started and develop the skills to get the most from JasperReports. The book has been fully updated to use JasperReports 3.5, the latest version of JasperReports. The previously accepted techniques that have now been deprecated have been replaced with their modern counterparts in this latest version.
All the examples in this book have been updated to use XML schemas for report templates. Coverage of new datasources that JasperReports now supports has been added to the book. Additionally, JasperReports can now export reports to even more formats than before, and exporting reports to these new formats is covered in this new edition of the book.

The book steers you through each point of report setup, to creating, designing, formatting, and exporting reports with data from a wide range of datasources, and integrating JasperReports with other Java frameworks.

What This Book Covers

Chapter 1, An Overview of JasperReports, introduces you to JasperReports and how it came to be. It gives you an insight to JasperReports’ capabilities and features, and also an overview of the steps involved in generating reports using JasperReports.

Chapter 2, Adding Reporting Capabilities to Java Applications, teaches you how to add reporting capabilities to your Java applications. You will have your development and execution environment set up to successfully add reporting capabilities to your Java applications by the end of this chapter.

Chapter 3, Creating Your First Report, shows you how to create, compile, and preview your first report in both JasperReports’ native format and web browser. It also briefs you about the JRXML elements corresponding to different report sections.

Chapter 4, Creating Dynamic Reports from Databases, continues with report creation, exploring how to create a report from the data obtained from a database. It also teaches you to generate reports that are displayed in your web browser in the PDF format.

Chapter 5, Working with Other Datasources, uses datasources other than databases, such as empty datasources, arrays or collections of Java objects, Maps, TableModels, XML, CSV files, and custom datasources to create reports, enabling you to create your own datasources as well.

Chapter 6, Report Layout and Design, gets you creating elaborate layouts, by controlling report-wide layout properties and styles, dividing the report data into logical groups, adding images, background text, and dynamic data to the reports, conditionally printing the report data, and creating subreports.

Chapter 7, Adding Charts and Graphics to Reports, takes you to more appealing reports by showing how to take advantage of JasperReports’ graphical features and create reports with graphical data like geometric shapes, images, and 2D and 3D charts.

Chapter 8, Other JasperReports Features, discusses the JasperReports features that lets you create elaborate reports, such as displaying report text in different languages, executing Java code snippets using scriptlets, creating crosstab reports, running a query with the results of a different query, adding anchors, hyperlinks, and bookmarks to the reports.

Chapter 9, Exporting to Other Formats, demonstrates how to export reports to the formats supported by JasperReports, such as PDF, RTF, ODT, Excel, HTML, CSV, XML, and plain text and how to direct the exported reports to a browser.

Chapter 10, Graphical Report Design with iReport, helps you get your hands on a graphical report designer called iReport, so that you can design reports graphically, and also, using iReport’s graphical user interface.

Chapter 11, Integrating JasperReports with Other Frameworks, explains how to integrate JasperReports with several popular web application frameworks and ORM tools, such as Hibernate, JPA, Spring, JSF, and Struts.

Graphical Report Design with iReport

So far, we have been creating all our reports by writing JRXML templates by hand. JasperSoft, the company behind JasperReports, offers a graphical report designer called iReport. iReport allows us to design reports graphically by dragging report elements into a report template and by using its graphical user interface to set report attributes.

iReport started as an independent project by Giulio Toffoli. JasperSoft recognized the popularity of iReport and, in October 2005, hired Giulio Toffoli and made iReport the official report designer for JasperSoft. Like JasperReports, iReport is also open source. It is licensed under the GNU Public License (GPL).
In 2008, iReport was rewritten to take advantage of the NetBeans platform. It is freely available both as a standalone product and as a plugin to the NetBeans IDE.

In this chapter, we will be covering the standalone version of
iReport; however, the material is also applicable to the iReport
NetBeans plugin.

By the end of this chapter, you will be able to:

  • Obtain and set up iReport
  • Quickly create database reports by taking advantage of iReport’s Report Wizard
  • Design reports graphically with iReport
  • Add multiple columns to a report
  • Group report data
  • Add images and charts to a report

Obtaining iReport

iReport can be downloaded from its home page at http://jasperforge.org/projects/ireport by clicking on the Download iReport image slightly above the center of the page.

Once we click on the image, we are directed to an intermediate page where we can either log in with our JasperForge account or go straight to the download page.

Either logging in or clicking on the No Thanks, Download Now button takes us to the iReport download page.

The standalone iReport product is in the first row of the table on the page. To download it, we simply click on the Download link in the last column. Other downloads on the page are for older versions of JasperReports, iReport NetBeans plugin, and other JasperSoft products.

iReport can be downloaded as a DMG file for Macintosh computers, as a Windows installer for Windows PCs, as a source file, as a ZIP file, or as a gzipped TAR file.

To install iReport, simply follow the usual application installation method for your platform. If you chose to download the ZIP or gzipped TAR file, simply extract it into any directory. A subdirectory called something like iReport-nb-3.5.1 will be created. (The exact name will depend on the version of iReport that was downloaded.) Inside this directory, you will find a bin subdirectory containing an executable shell script called ireport and a couple of Windows executables, ireport.exe and ireport_w.exe. On Windows systems, either EXE file will start iReport.

The difference between the two Windows executables is that the
ireport.exe will display a command-line window when iReport
is executed, and ireport_w.exe won’t. Both versions provide
exactly the same functionality.

On Unix and Unix-like systems, such as Linux and Mac OS, iReport can be started by executing the ireport shell script. The following screenshot illustrates how iReport looks when it is opened for the first time:

Setting up iReport

iReport can help us quickly generate database reports. To do so, we need to provide it with the JDBC driver and connection information for our database. iReport comes bundled with JDBC drivers for several open source relational database systems, such as MySQL, PostgreSQL, HSQLDB, and others. If we want to connect to a different database, we need to add the JDBC driver to iReport’s CLASSPATH. This can be done by clicking on Tools | Options and then selecting
the Classpath tab.

To add the JDBC driver to the CLASSPATH, click on the Add JAR button, and then navigate to the location of the JAR file containing the JDBC driver. Select the JAR file and click on the OK button at the bottom of the window.

We won’t actually add a JDBC driver, as we are using MySQL for
our examples, which is one of the RDBMS systems supported out of
the box by iReport. The information just provided is for the benefit of
readers using an RDBMS system that is not supported out of the box.

Before we can create reports that use an RDBMS as a datasource, we need to create a database connection. In order to do so, we need to click on the Report Datasources icon in the toolbar:

After doing so, the Connections / Datasources configuration window should
pop up.

To add the connection, we need to click on the New button, select Database JDBC connection, and then click on the Next> button.

We then need to select the appropriate JDBC driver, fill in the connection information, and click on the Save button.

Before saving the database connection properties, it is a good idea to click on the Test button to make sure we can connect to the database. If we can, we should see a pop-up window like the following:

After verifying that we can successfully connect to the database, we are ready to create some database reports.

Creating a database report in record time

iReport contains a wizard that allows us to quickly generate database reports (very useful if the boss asks for a report 15 minutes before the quitting time on a Friday!). The wizard allows us to use one of the predefined templates that are included with iReport. The included report templates are divided into two groups: templates laid out in a “columnar” manner and templates laid out in a “tabular” manner. Columnar templates generate reports that are laid out in columns, and tabular templates generate reports that are laid out like a table.

In this section, we will create a report displaying all the aircraft with a horsepower of 1000 or more. To quickly create a database report, we need to go to File | New | Report Wizard.

We should then enter an appropriate name and location for our report and click on Next>.

Next, we need to select the datasource or database connection to use for our report. For our example, we will use the JDBC connection we configured in the previous section. We can then enter the database query we will use to create the report.

Alternatively, we can use the iReport query designer to design the query.

For individuals with SQL experience, in many cases it is easier
to come up with the database query in a separate database client
tool and then paste it in the Query text area than using the
query designer.


The complete query for the report is:

	select
	a.tail_num,
	a.aircraft_serial,
	am.model as aircraft_model,
	ae.model as engine_model
	from aircraft a, aircraft_models am, aircraft_engines ae
	where a.aircraft_model_code = am.aircraft_model_code
	and a.aircraft_engine_code = ae.aircraft_engine_code
	and ae.horsepower >= 1000

The following window shows a list of all the columns selected in the query, allowing us to select which ones we would like to use as report fields:

In this case, we want the data for all columns in the query to be displayed in the report. Therefore, we select all columns by clicking on the second button.
We then select how we want to group the data and click on Next>. This creates a report group. (Refer to the Grouping Report Data section in Chapter 6, Report Layout and Design for details.)

In this example, we will not group the report data. The screenshot illustrates how the drop-down box contains the report fields selected in the previous step. We then select the report layout (Columnar or Tabular). In this example, we will use the Tabular Layout.

After selecting the layout, we click on Next> to be presented with the last step.
We then click on Finish to generate the report’s JRXML template.

While the template is automatically saved when it is created, the
report generated by the Preview button is not automatically saved.

We can then preview our report by clicking on Preview.

That’s it! We have created a report by simply entering a query and selecting a few options from a wizard.

Tweaking the generated report

Admittedly, the report title and column headers of our report need some tweaking. To modify the report title so that it actually refl ects the report contents, we can either double-click on the report title on iReport’s main window and type an appropriate report title, or we can modify the value of the Text property for the title static text in the Properties window at the lower righthand side.

Double-clicking on the title is certainly the fastest way to modify it. However, the Properties window allows us to modify not only the text, but also the font, borders, and several other properties.
We can follow the same procedure for each column header. The following screenshot shows the resulting template as displayed in iReport’s main window:

We’ll preview the report one more time to see the final version.

There you have it! The boss can have his or her report, and we can leave work and enjoy the weekend!

Creating a report from scratch

In the previous section, we discussed how to quickly generate a database report by using iReport’s Report Wizard. The wizard is very convenient because it allows us to create a report very quickly. However, its disadvantage is that it is not very fl xible.

In this section, we will learn how to create a report from scratch in iReport. Our report will show the tail number, serial number, and model of every aircraft in the FlightStats database.
To create a new report, we need to go to the File | New | Empty report menu item.

At this point, we should enter a Report name and Location.

In this example, we will set the report name to iReportDemo and accept all the other default values. After clicking on the OK button, iReport’s main window should look like this:

The horizontal lines divide the different report sections. Any item we insert between any two horizontal lines will be placed in the appropriate report section’s band. Horizontal lines can be dragged to resize the appropriate section(s). The vertical lines represent the left and right report margins. It is not possible to drag the vertical lines. To modify the left and right margins, we must select the report in the Report Inspector window at the top left.

Then, we need to modify the margins from the Properties window at the bottom right.

Properties for all the report sections and elements, such as variables,
scriptlets, title, background, detail, and so on, can be modified by
following the approach described here.

Going back to our empty report template, let’s add a report title. For this, we will use the static text Aircraft Report. To add the static text, we need to use the Static Text component in the Palette.

We then need to drag the Static Text component to the Title area of the report. iReport, by default, inserts the text Static text inside this field. To modify this default text, we can double-click anywhere inside the field and type in a more appropriate title. Alternatively, we can modify the Text property for the static text field in the Properties window at the lower righthand side.

In the Properties window, we can modify other properties for our text. In the above screenshot, we modified the text size to be 18 pixels, and we made it bold by clicking on the checkbox next to the Bold property. We can center the report title within the Title band by right-clicking on it, selecting Position, and then Center.

After following all of these steps, our report should now look like this:

Applying the same techniques used for adding the report title, we can add some more static text fields in the page header. After adding the page header, our report now looks like this:

We modified the Vertical Alignment of all three text fields in the page header by selecting the appropriate values in the Properties window for each one of them.

Now it is time to add some dynamic data to the report. We can enter a report query selecting the report node in the Report Inspector window and then selecting Edit Query.

As we type the report query, by default iReport retrieves report fields from it. This query will retrieve the tail number, serial number, and model of every aircraft in the database.

Now that we have a query and report fields, we can add text fields to the report. We can do so by dragging the fields in the Report Inspector window to the appropriate location in the report template.

After aligning each text field with the corresponding header, our report should now look like this:

To avoid extra vertical space between records, we resized the Detail band by dragging its bottom margin up. The same effect can be achieved by double-clicking on the bottom margin.
Notice that we have an empty Column Header band in the report template. This empty band will result in having some whitespace between each header and the first row in the Detail band. To avoid having this whitespace in our report, we can easily delete this band by right-clicking on it in the Report Inspector window and selecting Delete Band.

We now have a simple but complete report. We can view it by clicking on Preview.

That’s it! We have created a simple report graphically with iReport.

Creating more elaborate reports

In the previous section, we created a fairly simple database report. In this section, we will modify that report to illustrate how to add images, charts, and multiple columns to a report. We will also see how to group report data. We will perform all of these tasks graphically with iReport.

Adding images to a report

Adding static images to a report is very simple with iReport. Just drag the Image component from the Palette to the band where it will be rendered in the report.

When we drop the image component into the appropriate band, a window pops up asking us to specify the location of the image file to display.

After we select the image, we can drag it to its exact location where it will be rendered.
As we can see, adding images to a report using iReport couldn’t be any simpler.

Adding multiple columns to a report

The report we’ve been creating so far in this chapter contains over 11,000 records. It spans over 300 pages. As we can see, there is a lot of space between the text fields. Perhaps it would be a good idea to place the text fields closer together and add an additional column. This would cut the number of pages in the report by half. To change the number of columns in the report, we simply need to select the root report node in the Report Inspector window at the top left and then modify its Columns property in the Properties window at the bottom right.

When we modify the Columns property, iReport automatically modifies the Column Width property to an appropriate value. We are free, of course, to modify this value if it doesn’t meet our needs.

As our report now contains more than one column, it makes sense to re-add the Column Header band we deleted earlier. This can be done by right-clicking on the band in the Report Inspector window and selecting Add Band.

Next, we need to move the static text in the page header to the Column Header band. To move any element from one band to another, all we need to do is drag it to the appropriate band in the Report Inspector window.

Next, we need to resize and reposition the text fields in the Detail band and the static text elements in the Column Header band so that they fit in the new, narrower width of the columns. Also, resize the Column Header band to avoid having too much whitespace between the elements of the Column Header and Detail bands. Our report now looks like this:

We can see the resulting report by clicking on Preview.

Grouping report data

Suppose we are asked to modify our report so that data is divided by the state where the aircraft is registered. This is a perfect situation to apply report groups. Recall from Chapter 6, Report Layout and Design, that report groups allow us to divide report data when a report expression changes. Recall that our report query limits the result set to aircraft registered in the United States, and one of the columns it retrieves is the state where the aircraft is registered.

To define a report group, we need to right-click on the root report node in the Report Inspector window, and then select Add Report Group.

Then, enter the Group name and indicate whether we want to group by a field or by a report expression. In our case, we want to group the data by state field.

After clicking on Next>, we need to indicate whether we want to add a group header and/or footer to our report.

For aesthetic purposes, we move the static text fields in the Column Header band to the Group Header band, remove the column and page header bands, and add additional information to the Group Header band. After making all of these changes, our report preview will look like this:

We can preview the report by clicking Preview.

Adding charts to a report

To add a chart to a report, we need to drag the Chart component from the Palette into the approximate location where the chart will be rendered in the report.

When dropping the chart component into the report, the following window will pop up, allowing us to select the type of chart we want to add to the report:

For this example, we will add a 3D bar chart to the report. All that needs to be done is to click on the appropriate chart type, and then click on the OK button.

Our chart will graphically illustrate the number of aircraft registered in each state of the United States. (We will explain how to have the chart display the appropriate data later in this section.) We will place the chart in the Summary band at the end of the report. As the chart will illustrate a lot of data, we need to resize the Summary band so that our chart can fit. After resizing the Summary band, outlining the area of the report to be covered by the chart, and selecting the chart type, the Summary section of our report preview looks like this:

To fine-tune the appearance of the chart, we can select it in the Report Inspector window and then modify its properties as necessary in the Properties window.

To specify the data that will be displayed in the chart, we need to right-click on the chart in the Report Inspector window and select Chart Data. We then need to click on the Details tab in the resulting pop-up window.

We then need to click on the Add button to add a new Category series.

The Series expression field is the name of the series. Its value can be any object that implements java.lang.Comparable. In most cases, the value of this field is a string. The Category expression field is the label of each value in the chart. The value of this field is typically a string. In our example, each state is a different category, so we will use the state field ($F{state}) as our category expression.

The Value expression field is a numeric value representing the value to be charted for a particular category. In our example, the number of aircraft in a particular state is the value we want to chart. Therefore, we use the implicit stateGroup_COUNT variable ($V{stateGroup_COUNT}) as our value expression.

The optional Label Expression field allows us to customize item labels in the chart.

Every time we create a group in a report template, an implicit variable named groupName_COUNT is created, where groupName is the name of the group.

We can either type in a value for the Series expression, Category expression, and Value expression fields, or we can click on the icon to be able to graphically select the appropriate expression using iReport’s Expression editor.

Using the Expression editor, we can select any parameter, field, or variable as our expression. We can also use user-defined expressions to fill out any of the fields that require a valid JasperReports expression.

After selecting the appropriate expressions for each of the fields, our chart details are as follows:

After clicking on OK and closing the Chart details window, we are ready to view our chart in action, which can be done simply by clicking on Preview.

Help and support

Although this chapter didn’t discuss every iReport feature, I’m confident that iReport is intuitive enough after you get comfortable with it. Some of the iReport features not covered in this chapter include subreport creation and adding crosstabs, lines, ellipses, and rectangles to a report. However, we have learned all these features the “hard” way by creating a JRXML template by hand. For someone familiar with JasperReports, adding these features to a report created by iReport should be trivial. If more help is needed, JasperSoft provides additional documentation for iReport, and lots of knowledgeable people frequent the iReport forums at http://jasperforge.org/plugins/espforum/browse.php?group_id=83&forumid=101.

Summary

This chapter taught us how to install and set up iReport, use iReport’s Report Wizard to quickly generate a report, and graphically design custom reports. Moreover, we learned how to group report data graphically with iReport, to add multiple columns to a report, and to add images and charts to a report graphically with iReport. iReport is a very powerful tool that can significantly reduce report design time. To use all of the features of iReport effectively, however, an iReport user must be familiar with basic JasperReports concepts, such as bands, report variables, report fields, and so on.

Comments

comments

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better