Graphical Report Design with iReport: Part 1

Exclusive offer: get 50% off this eBook here
JasperReports 3.5 for Java Developers

JasperReports 3.5 for Java Developers — Save 50%

Create, Design, Format, and Export Reports with the world's most popular Java reporting library

$26.99    $13.50
by David R. Heffelfinger | August 2009 | Java Open Source

In this two-part article by David Heffelfinger, instead of writing JRXML templates by hand, we will learn to design reports graphically by dragging report elements into a report template and by using its graphical user interface to set report attributes. In this first part, we will help you set up iReport on your machine and get you started with designing graphical reports.

JasperSoft, the company behind JasperReports, offers a graphical report designer called iReport. 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 article, 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 article, 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

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.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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 theireport.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:

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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:

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

After selecting the layout, we click on Next> to be presented with the last step.

Graphical Report Design with iReport: Part 1

We then click on Finish to generate the report's JRXML template.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

 

JasperReports 3.5 for Java Developers Create, Design, Format, and Export Reports with the world's most popular Java reporting library
Published: August 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

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 reflects 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.

Graphical Report Design with iReport: Part 1

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:

 

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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 flexible. 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 theflightStats database. For details of the database used for this example, visit Database for out Reports section of the Creating Dynamic Reports from Databases article.

To create a new report, we need to go to the File | New | Empty report menu item.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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:

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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, selectingPosition, and then Center.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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:

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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 selectingEdit Query.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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.

Graphical Report Design with iReport: Part 1

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

Graphical Report Design with iReport: Part 1

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

Summary

In this first part of the article, we learned how to install and set up iReport, use iReport's Report Wizard to quickly generate a report, and graphically design custom reports. In the next part, we will focus on creating more elaborate reports by using iReport's graphical user interface.

 

 

JasperReports 3.5 for Java Developers Create, Design, Format, and Export Reports with the world's most popular Java reporting library
Published: August 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


David R. Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing and developing software professionally since 1995 and has been using Java as his primary programming language since 1996. He has worked on many large scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He has a Masters degree in Software Engineering from Southern Methodist University. David is editor in chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.

Books From Packt

  WebSphere Application Server 7.0 Administration Guide
WebSphere Application Server 7.0 Administration Guide

WordPress MU 2.7: Beginner's Guide
WordPress MU 2.7: Beginner's Guide

Spring 2.5 Aspect Oriented Programming
Spring 2.5 Aspect Oriented Programming

Pentaho Reporting 3.5 for Java Developers
Pentaho Reporting 3.5 for Java Developers

Grails 1.1 Web Application Development
Grails 1.1 Web Application Development

Drools JBoss Rules 5.0 Developer's Guide
Drools JBoss Rules 5.0 Developer's Guide

jQuery UI 1.6: The User Interface Library for jQuery
jQuery UI 1.6: The User Interface Library for jQuery

LWUIT 1.1 for Java ME Developers
LWUIT 1.1 for Java ME Developers

 

 

No votes yet
problem by
can u tell me ho how to create reports in ireports in java net beans on selection of one option from many and on just button click it shows report
How to create a drop down list in jasper ireport by
Hi I am using jasper ireport im unable to create a dropdown list .My scenario is i should have years in dropdownlist depending on the year selected i should show the data for corresponding year.I was searching for it from so many days.
Appreciation by
David Heffelfinger, thank you for this tutorial. it is precisely very straight forward. if you would mind i would like to be your friend in Java Language programming and related technologies. Okpali Gabriel Head Development Unit Panet Technologies Ltd www.panettech.com you can reach me via child2007eje@yahoo.com

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
h
Z
n
n
T
5
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software