Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Saturday, March 10, 2012

Dynamic Crosstab Sorting in Jasper Reports

You can drive the order of Row Groups and Column Groups in a Jasper Reports Crosstab using a Parameter and a custom Java class.

This video shows how to control the sort order of a Jasper Reports Crosstab using a Parameter.


Java Class Notes

To use the Java class mentioned in the example, create a toplevel directory like C:/classes.  Next, add the required folder structure for the Java class (create folders for each path elements): com/bekwam/blog/sort.  Finally, download the Java class file from the Bekwam web server:

DynaSortComparator.class

Alternatively, compile the Java class available here: DynaSortComparator.java.

Next, point iReport to the toplevel class folder you created.  DO NOT SET THE CLASSPATH TO THE com, bekwam, blog, or sort subfolders.

Setting Classpath in iReport
This classpath setting and the class file will need to be replicated if you deploy outside of iReport like in a webapp or Jasper Server.

Possible Errors

The .class file I provided works only with Java 1.7.  I'll put up a backward-compatible version, but you can resolve this yourself by compiling your own version if you have a JDK available.


To check if a JDK is available type 'javac  version' at a command prompt.

Here are the instructions on compiling DynaSortComparator.java for your version.


1. Save the source file in the "sort" folder created for the class file. (com/bekwam/blog/sort)
2. Change into the sort folder
3. Run the command > javac DynaSortComparator.java


This will replace the downloaded class file with one specific to your system.

2 comments:

  1. Thanks for this great tutorial. I wrote you at gmail, if you could help me, it would be great.

    ReplyDelete
    Replies
    1. Kalina wrote about ordering a set of columns not involved in the crosstab. These are classifier columns that gave the crosstab columns more context. For example, a report with classifier columns Dept and Employee and then a list of Item columns. I suggested creating multiple reports, one report for each classifier column. This would produce one report that is Dept/Employee/Item1...ItemN and one report Employee/Dept/Item1...ItemN.

      This has the downside of hampering maintenance because there is now more similar code to maintain. However, if you're close to a deadline, this may be something you can use if the dynamic behavior isn't readily apparent.

      Delete