["Thank you" to the blog reader who posted this solution here.]
In a previous post, I described how a Text Field can be added to a Row Group in a JasperReports Crosstab in order to provide a sequence number. I suggested developing a small Java class that would be incremented with each evaluation of the Text Expression. As each Row Group was printed, the current value of the Text Expression would be printed and incremented: 1, 2, 3.
However, this recommendation had the downside of requiring a Java class. If you're not familiar with Java, this might be a major obstacle. Even if are familiar with Java, developing a class and carrying a JAR file throughout your deployments (the packaging of a Java class) isn't desirable. This is especially true when you can add this functionality to your report with a few variable definitions.
This Crosstab has a Row Group 'Year'. And with each Year (2008-2011), there is a sequence printed (1-4). This post shows how to create a Crosstab.
|A Crosstab with a Year Row Group and a Sequence|
To add the sequence, start by adding a Report Group to the report. In this example, the Crosstab is added to the Summary Band. Adding a Report Group "YEAR_GROUP" will create a Report Variable "YEAR_GROUP_COUNT".
Next, add your own variable, in this case "CROSSTAB_ROW_COUNTER". Set the values in accordance with the following screenshot. Notice the dimmed Group Bands for Year which aren't required in the report. CROSSTAB_ROW_COUNTER references YEAR_GROUP_COUNT.
|YEAR_GROUP_COUNTER Created via the Report Group Wizard and the User-Defined CROSSTAB_ROW_COUNTER|
Crosstabs lack the ability to inject Report Variables into their Text Field Expressions. However, Parameters and Measures can go into Text Field Expression and Parameters and Measures do have access to the global Report Variables. This Measure is defined to allow a Text Field this access. The Measure is called "ROW_COUNTER" and is linked to CROSSTAB_ROW_COUNTER.
|Defining a Measure to Provide Access to a Variable|
Finally, a Text Field is dragged onto the Crosstab Row Group. The Text Field is linked to the Measure ROW_COUNTER. It can't be directly linked to the outer CROSSTAB_ROW_COUNTER; you'll get a "not found" error if you try.
I'm glad to have learned this technique because it decreases the footprint of my Jasper Report. Keep the custom Java version of the counter in mind if you have a more complicated report layout and you want to evaluate an expression without regard to other structures (Report Groups, etc) in the report.