Search code examples
javajasper-reports

How to add collection of java bean in table in jaspersoft report


I am using jaspersoft for my report. I need to add my collection of java bean fields in the table which I got it using collection of java beans as the data adapter. I am able to display data that I get as java beans, but I am not finding a way to add those fields to the table in jaspersoft report. Please help I am been bugging around this from past few data still I am not iron out of this issue. Below is the source code on what i am trying.

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-23T15:07:03 -->
<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="javabeantest1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="78330407-cdc3-4758-b2a3-2ddae526bed9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test factory3"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="javabean" uuid="adf401bd-6c3e-4316-8ff1-285d86d56c48">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TestFactory1"/>
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="age" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="87" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="33" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="3" width="100" height="30" uuid="2c3c0b2c-ef69-423b-ba00-47bfb52dfade"/>
                <text><![CDATA[name]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="1" width="100" height="30" uuid="8c4db8c2-212f-4427-89b7-5eaf3091a1aa"/>
                <text><![CDATA[age]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="39fd9f41-27dd-406f-aa32-d6e930dc346a"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="1c916b19-cb2f-481c-8d0d-349eced5a8a2"/>
                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="249" splitType="Stretch">
            <componentElement>
                <reportElement x="290" y="49" width="200" height="200" uuid="0d721a59-5052-42a4-bd71-b61e51308115">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <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="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource() ]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="89affd55-2820-4860-a01c-ce794c5941a9">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="33b03723-89a8-4eba-b08a-e9360ecf3e08"/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="54e2604e-96f8-4e83-b506-ca2beeef5751"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="41fcbd02-87dc-4a72-8464-d9609e5e592a">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f4078108-0c94-406e-907f-d20a0932afeb"/>
                                <text><![CDATA[age]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b1250d44-7bb0-445f-8c46-ab6c880872a8"/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

Solution

  • Your java bean needs to be something like this (correspond to your subDataset named javabean).

    public class JavaBean {
      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;
      }
    }
    

    Then you need a List<JavaBean> of the JavaBean's.

    Lets imagine that you pass this List as a parameter during the fill process (in the parameter map with the key listOfJavaBeans).

    in jrxml define the parameter

    <parameter name="listOfJavaBeans" class="java.util.List"/> 
    

    To pass it to your table as datasource use.

    <datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfJavaBeans})]]></dataSourceExpression>
    </datasetRun>