14

I need to show JRBeanCollectionDataSource data in Table component (JasperReports).

Here is my template, ShowPerson.jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ShowPerson" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="304c4c4e-c99a-4399-8081-748d3b7c0b8c">
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Table Dataset 1" whenResourceMissingType="Empty" uuid="63b01547-bce2-47c9-ba15-666f94d11387">
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <parameter name="INFO" class="java.lang.String"/>
    <title>
        <band height="40" splitType="Stretch">
            <staticText>
                <reportElement uuid="e96450a8-8358-4cae-a094-3add06d57de2" x="0" y="20" width="56" height="20"/>
                <textElement/>
                <text><![CDATA[Info]]></text>
            </staticText>
            <textField>
                <reportElement uuid="e21e9932-ebfe-4bb5-904d-ea99e141866b" x="56" y="20" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{INFO}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="40" splitType="Stretch">
            <componentElement>
                <reportElement uuid="dea5d821-81b6-434b-ae27-4f3a0268e805" key="table 1" x="0" y="0" width="572" height="40"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="169" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
                        <jr:tableHeader height="20" rowSpan="1">
                            <staticText>
                                <reportElement uuid="795dac43-0ff0-482c-89a0-7dac3b27d513" x="0" y="0" width="169" height="20"/>
                                <textElement/>
                                <text><![CDATA[Name]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement uuid="4f1ab13a-d776-4cd5-a2a7-a5cf23360816" x="0" y="0" width="169" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="173" uuid="a6997eea-131e-4555-80e6-a20d4decb18f">
                        <jr:tableHeader height="20" rowSpan="1">
                            <staticText>
                                <reportElement uuid="5f6e2413-8025-47ca-9638-9609ea13f93f" x="0" y="0" width="173" height="20"/>
                                <textElement/>
                                <text><![CDATA[Age]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c" x="0" y="0" width="173" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

My POJO:

public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Main class for building report:

public class OpenReport {
    public static void main(String[] args) throws JRException {
        Map<String, Object> peopleMap = new HashMap<>();
        peopleMap.put("Sisco", 17);
        peopleMap.put("Eve", 19);
        peopleMap.put("John", 20);
        peopleMap.put("George", 21);
        peopleMap.put("Steve", 18);

        ArrayList<Person> dataList = new ArrayList<Person>();

        for(Map.Entry<String, Object> personMap : peopleMap.entrySet()) {
            Person person = new Person();
            person.setName(personMap.getKey());
            person.setAge(Integer.valueOf(personMap.getValue().toString()));
            dataList.add(person);
        }

        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
        Map parameters = new HashMap();
        parameters.put("INFO", "Hello");

        JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);

        JFrame frame = new JFrame("Report");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new JRViewer(jasperPrint));
        frame.pack();
        frame.setVisible(true);
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
Scooby
  • 262
  • 1
  • 3
  • 16
  • Possible duplicate of [JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?](http://stackoverflow.com/questions/12209300/jrbeancollectiondatasource-how-to-show-data-from-the-java-util-list-from-javabe) – Dave Jarvis May 13 '16 at 19:22

2 Answers2

23

1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty).

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
        Map parameters = new HashMap();
        parameters.put("INFO", "Hello");
        parameters.put("DS1", beanColDataSource);

        JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());

2.Decalre a new parameter in the report of type JRBeanCollectionDataSource

<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>

3.Set TableDatasource to use the $P{DS1} parameter.

<jr:table ...>
    <datasetRun subDataset="Table Dataset 1">
        <datasetParameter name="REPORT_DATA_SOURCE">
             <datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
        </datasetParameter>
    </datasetRun>
.....

4.Declare the fields (name, age) in Table Dataset 1.

5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively).

Laura
  • 1,552
  • 12
  • 12
  • Thanks @laura. I have update my code and question. Java parsed 5 data, but jasper only showing 4 data. Did i miss something in the code? – Scooby Mar 12 '14 at 12:31
  • 2
    Sorry, I forgot about this behavior of Jasper. The problem is not in your code, it's just how jasper works, the record pointer in the data source is incremented by every element that receives it (so you have report and table, so it will skip the first record). I updated my answer with the solution I usually use for this kind of situations. – Laura Mar 12 '14 at 13:34
  • Don't use P_REPORT_DATA_SOURCE as parameter of dataSet. Use collection as parameter of report (it's not beanColDataSource, it's must be dataList) See http://stackoverflow.com/questions/18787697/table-tool-in-showing-one-fewer-lesser-database-records/18822712#18822712 – sanBez Mar 12 '14 at 14:48
  • 1
    Excellent Steps to catch easy – Senthil Muthiah Jul 19 '14 at 19:21
  • 1
    I use Japser Report version 6.14.0, I pass the parameters to the report and pass that to the Table using the method describes above. it works. One point that I would like to mention is if new JREmptyDataSource is not being used and use new JRBenaCollectionDataSource instead. The first item in your list, which is passed as parameters would be skipped. For example, you have 4 records in your list, the output of report will output 3 records only. – jkwli Nov 27 '20 at 08:18
4

@laura: I get new error when trying set the TableDatasource to use the $P{DS1} parameter. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type

Now, I have found the solution, maybe not the best.

Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", dataList);

JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());

Modify the class of $P{DS1} to java.util.Collection or java.util.List

And set the table datasource to

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS1})
sanBez
  • 933
  • 1
  • 8
  • 18
Scooby
  • 262
  • 1
  • 3
  • 16