2

I'm losing a lot of hair trying to learn how to use Jasper reports. Essentially, I have about five java.util.List objects, each typed to a different data type which represents my data:

public class Person { 

    private int id;

    private String firstName;

    private String lastName;

    private int age;

    // accessors/mutators omitted for brevity
}

public class Place {

    private int id;

    private String name;

    private String type;

    private String state;

    // accessors/mutators omitted for brevity
}

public class Thing {

    private int id;

    private String name;

    private int rating;

    // accessors/mutators omitted for brevity
}  

I'd simply like to display my data in tables somewhat like this:

a

It seems that Jasper Reports is really only designed to work with one data source, unfortunately. I've been trying to figure this out for a while now and I've hit a wall. How can I have multiple datasources and tables representing said datasources inside a Jasper Report *.jrxml file?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • You can read the answer by GenericJon on [this question](http://stackoverflow.com/q/7482412/876298) – Alex K Mar 14 '12 at 08:55

2 Answers2

2

What you could do is use subreports. Create a master report with the title section you want. In the master report you will create three parameters that are JRDatasources, one for each subreport. Then you create each subreport for each of the tables you want.

Then you add each as a subreport in the main report, you may have to play with it, but off the top of my head I think the best place to put is probably the Summary. When using ireport it will prompt you with an option to pass a parameter as the datasource for the subreport.

Ideally there would be a way to tie a parameter to a detail section, since in iReport you can have multiple detail sections now. Unfortunately I have not found a way to do that, if anybody knows a way please downvote and add an answer.

Although there is a second option where you could create your own custom datasource and multiple detail sections in a single report. Essentially you would end up creating a new object that combines them all into one and .... nevermind that is a horrible idea.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
  • How do I wrap my `List` in a data source in the JRXML template? Also, how do I declare a parameter? I've been nothing but confused since I started looking at this library. – Naftuli Kay Mar 14 '12 at 00:48
  • I want to note that putting a sub-report in "Page Header" band might cause some problems if the sub-report actually took more than one page. "Detail" or "Summary" band is more suitable. – Rangi Lin Mar 14 '12 at 00:57
  • 1
    @TKKocheran > I think you need [JRBeanCollectionDataSource](http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRBeanCollectionDataSource.html). For each sub-report, you need to provide it a data source, which can be a parameter of your main report. – Rangi Lin Mar 14 '12 at 01:07
  • Rangi is correct about wrapping each one in the JRBeanCollectionDataSource. As far as the the section, Summary would probably be best. I always forget which one to put it in, and I do not have iReport infront of me at the moment. So thanks @Rangi. – Jacob Schoen Mar 14 '12 at 01:12
1

I did this with the list element of jasper reports and pass the data source as a parameter to the report. Make sure to put the list in a band which can grow otherwise your content may be truncated.

            <componentElement>
                <reportElement x="0" y="321" width="442" height="13" isRemoveLineWhenBlank="true"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="produkt">
                        <dataSourceExpression><![CDATA[$P{produkteDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="13" width="442">
                        <!-- Content goes here -->
                    </jr:listContents>
                </jr:list>
            </componentElement>
sclassen
  • 138
  • 3