Extreme Java When vanilla Java is not enough


Jasper Reports tutorial – part 2 – tables

I never planned a "part 2" about Jasper Reports, but the previous "tutorial" lacks a small concept: how do I make tables? It's obvious, but only after you figure it out. If you downloaded Jasper Reports examples, you can look at "datasource" sample. Nice one, but not as simple as you may want.

I only want to add a basic table which row data comes from a Java Collection. It is very easy to adapt the JasperFiller: just change JREmptyDatasource with JRBeanCollectionDataSource.

Adapting JRXML is easy, too, but it not obvious for a newbie. I admit I expected something like a "table" element (blame years of HTML), but in JR, you only need to define the "detail" element with the contents you want to repeat for each element on DataSource. Create it as you want, tune the band height to the height of a row, and you have your HTML-ish table.

BTW, you need to define all fields at the top of your JRXML, since JR's DataSources does not contains metadata.

As a last tip, keep these in mind:

  • <title> only appears on first page;
  • <pageHeader> and <pageFooter> appears on every page;
  • <lastPageFooter> apperars only on last page, REPLACING <pageFooter>

Jasper Reports (quick and dirt) tutorial

If you want a simple Jasper Reports tutorial, you will have a bad time googling it. The official site has a nice theorical introduction, but after reading it, you will not have a working "hello world" example. Unfortunatelly, the "definitive guide" is a book you need to buy.

Since I prefer a "try-before-you-buy" approach, I asked Google and I've found a simple "hello world" JRXML reading this tutorial from David Heffelfinger's article. Good one, but I will upgrade it. The XML can use XSD:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport name="report name"

If you are using NetBeans 6, go to "Tools > Options > Miscellaneous > Files", add "jrxml" extension and associate with "application/xml". Now, it will autocomplete jasper tags for you. You can use iReport NB plugin, too, but I like to know how it work.

Well, you need to compile this JRXML before using it. David shows how to do programmatically. If you want to add only the compiled version to your JAR file, you can add this to your Ant build file (this is a NB version, change parameters as needed):

<target name="-post-init">
  <!-- Init jasper ant task -->
  <taskdef classpath="${javac.classpath}"
      name="jrc" />
<target name="-post-compile">
  <!-- without a tempdir, JR may
       create files on NB folder! -->
  <mkdir dir="${build.dir}/jasper"/>
  <!-- compile "src/JRXML" to "build/JASPER" -->
  <jrc destdir="${build.classes.dir}"
      <fileset dir="${src.dir}" includes="**/*.jrxml" />
      <path path="${javac.classpath}"/>

Now you have a ".jasper" file on your build dir, together with your compiled classes - just ready for use! Let's use it:

InputStream in = getClass().getResourceAsStream("/my-pkg/myReport.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(in,
        new HashMap(), new JREmptyDataSource());

Pretty easy, huh? One last note - you will need more libs than just "jasperreports.jar" on your classpath:

  • commons-digester
  • commons-logging
  • commons-collections
  • commons-beanutils
  • itext (only if you want to export as PDF)