JasperReports 3.6: Creating a Report from Model Beans of Java Applications

If you’re a Java developer who wants to make the most of the world’s most popular open source reporting engine, this book is all you need: a step-by-step course in creating super-attractive, user-friendly business reports.

(For more resources on JasperReports, see here.)

Getting ready

You need a Java JAR file that contains class files for the JavaBeans required for this recipe. A custInvoices.jar file is contained in the source code (chap4). Unzip the source code file for this article and copy the Task5 folder from the unzipped source code to a location of your choice.

How to do it...

Let's start using Java objects as data storage units.

  1. Open the ModelBeansReport.jrxml file from the Task5 folder of the source code for this article (chapt 4). The Designer tab of iReport shows a report containing data in the Title, Column Header, Customer Group Header1 and Detail 1 sections, as shown in the following screenshot:

    JasperReports 3.6

  2. If you have not made any database connection so far in your iReport installation, you will see an Empty datasource shown selected in a drop-down list just below the main menu. Click on the Report Datasources icon, shown encircled to the right of the drop-down list in the following screenshot:

    JasperReports 3.6

  3. A new window named Connections / Datasources will open, as shown next. This window lists an Empty data source as well as the datasources you have made so far.

    JasperReports 3.6

  4. Click the New button at the top-right of the Connections / Datasources window. This will open a new Datasource selection window, as shown in the following screenshot:

    JasperReports 3.6

  5. Select JavaBeans set datasource from the datasource types, as shown next. Click the Next button.

  6. A new window named JavaBeans set datasource will open, as shown in the following screenshot:

    JasperReports 3.6

  7. Enter CustomerInvoicesJavaBeans as the name of your new connection in the text box beside the Name field, as shown in the following screenshot:

    JasperReports 3.6

  8. Enter com.CustomerInvoicesFactory as the name of the factory class in the text box beside the Factory class field, as shown in the following screenshot:

    JasperReports 3.6

    This com.CustomerInvoicesFactory class provides iReport with access to JavaBeans that contain your data.

  9. Enter getBeanCollection as the name of the static method in the text box beside The static method... field, as shown in the following screenshot:

    JasperReports 3.6

  10. Leave the rest of the fields at their default values. Click the Test button to test your new connection to the JavaBeans datasource. You will see an Exception message dialog.
  11. This exception message occurs because iReport can't find your factory class. Dismiss the message box by clicking OK.
  12. Click the Save button at the bottom of the JavaBeans set datasource window and close the Connections / Datasources window as well.

(For more resources on JasperReports, see here.)

  • Click Tools from the main iReport menu and select Options from the drop-down list, as shown in the following screenshot:

    JasperReports 3.6

  • A window named Options will open, as shown in the following screenshot:

    JasperReports 3.6

  • Click on the Classpath tab. You will see a default classpath in a path table, as shown below. Click the Add JAR button on the top-right part of the Classpath tab.

    JasperReports 3.6

  • A file browser will appear. Browse to the Task5 folder that you copied in the Getting ready section. Select the custInvoces.jar file and click the Open button, as shown in the following screenshot:

    JasperReports 3.6

  • The path to the JAR file containing JavaBeans will be added to the path table, as shown next. Click the OK button at the bottom of the Options window.

    JasperReports 3.6

  • Click on the Report Datasources icon to the right of the datasources drop-down list just below the main menu, as shown encircled in the following screenshot. A new window named Connections / Datasources will open, as shown next. It will show a list of available connections. Select the CustomerInvoicesJavaBean connection and click the Modify button.

    JasperReports 3.6

  • The JavaBeans set datasource window of step 6 will open. Click on the Test button at the bottom of the window. You will see a Connection test successful! message dialog. Click OK to dismiss it.
  • Click the Save button at the bottom of the JavaBeans set datasource window to save the connection settings.
  • You will see a Connections / Datasources window with the CustomerInvoicesJavaBean connection set as default. Click the close icon on the top-right corner of the window to close it.
  • Click the Report query button on the right of the Preview tab. A Report query dialog will appear, as shown in the following screenshot:

    JasperReports 3.6

  • Click on the JavaBeans Datasource tab. The JavaBeans Datasource tab will appear, as shown in the following screenshot:

    JasperReports 3.6

  • Enter com.CustomerInvoice as name of the class in the text box beside the Class name field. Then click the Read Attributes button. It will read the fields of the com.CustomerInvoice class and list them in the text area below the Class name field, as shown in the following screenshot:

    JasperReports 3.6

  • Select all the fields of the com.CustomerInvoice class except the class field, as shown in the in the following screenshot. Click the Add selected field(s) button at the bottom of the text area containing the fields list. This will show selected fields in the lower-half of the Report query dialog. Click the OK button.

    JasperReports 3.6

  • Switch to the Preview tab and you will see all the invoices listed in the report grouped by customer names.
  • How it works...

    In this article you have learned how to create a connection to JavaBeans and then use the data stored in the beans to generate your report. For this purpose, you did four things:

    1. You configured the name of a factory class in step 8 of the recipe.
    2. The factory class should have a static method to instantiate or access your JavaBeans. You configured the name of this static method in step 9 of the recipe.
    3. You provided a classpath to a JAR file, which contains the factory class as well as a class for the JavaBeans in step 16.
    4. Then you configured the properties of your JavaBeans as fields in iReport in steps 23 to 25. This enables iReport to fetch data from your JavaBeans.

    JasperReports internally calls the getBeanCollection() static method of the factory class. This method should contain the Java code that returns the actual JavaBeans from which you want to generate the report. For the sake of simplicity, I have returned a hardcoded object as shown in the following piece of code:

    public static Vector getBeanCollection() {
    CustomerInvoice invoice001 = new CustomerInvoice (
    1001,"Packt Publishing","Packing Material","Mar09",5020);
    CustomerInvoice invoice002 = new CustomerInvoice (
    1002,"Packt Publishing","Offset Paper","Apr09",3000);
    //Other instance creation calls to the CustomerInvoice class
    Vector invoices = new Vector();

    invoices.add(invoice001);
    invoices.add(invoice002);
    //more calls to invoices.add() method

    return invoices;
    }

    Summary

    With this we complete the four part article series which was focused on advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once.


    Further resources on this subject:


    Books to Consider

    comments powered by Disqus
    X

    An Introduction to 3D Printing

    Explore the future of manufacturing and design  - read our guide to 3d printing for free