This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes Creating a Data Table in Jasper Report Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Creating a Data Table in Jasper Report" Watch "Creating a Data Table in Jasper Report" New topic

Creating a Data Table in Jasper Report

Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211

I am using Jasper Reports and I am wondering how you can create a data-table-like element in your reports. What I want to do basically is create a report which display records in a data table. The records may vary in size (i.e. the number of rows may differ), so hardcoding the records into static XML elements is not an option. The data of the records by the way is coming from a Java Bean datasource, and not from a database query. I know Jasper Reports has no built-in element to handle data-table-like displays, which is quite frustrating and surprising since most reports require some tabular data for display. Can somebody please help, it will be greatly appreciated. Thanks in advance.


There's so much to learn in this industry, and not everybody has the necessary interest.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

JasperReports handles this quite well, actually. This is fairly easy is the number of rows is all that is changing. (If the columns change as well, you can fake it with a cross-tabulation report, but that is frustrating to work with in any reporting system).

Usually, I create a List of records in a subclass of the JRDataSource class and pass that datasource to the report. The Details section of the report is then repeated once for each record returned by the call to .next() in the datasource. Yyou have to implement the stepping-though-the-list code and the get-value-for-the-field code youself; I usually do something like:

Then, I implement the getFieldValue as basically a big switch method -- if the field name is "FOO" I return the value data.getFoo(). (Or, most of the time I just create lists of Maps and simply return data.get(fieldName));

There you go -- instant data table.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Wow! Thanks Joel this worked great for me. Cheers man!

I agree. Here's the link:
subject: Creating a Data Table in Jasper Report
It's not a secret anymore!