Search code examples
javajasper-reportsdatasource

JRBeanCollectionDatasource prints only first bean's propery values


I'm working on a simple JRBeanCollection example, i am just trying to print all the property values of a javabean collection to a pdf report.

The problem is that my code prints only those of the first bean of the list i create.

Here is all the code i have written,

public static void main(String[] args) {
    String fileName = "src/test/report2.jasper";
    String outFileName = "test.pdf";
    HashMap hm = new HashMap();
    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(FundBeanFactory.createBeanCollection());
    try {
        JasperPrint print = JasperFillManager.fillReport(
                fileName,
                hm,
                beanCollectionDataSource);

        JRPdfExporter exporter = new JRPdfExporter();

        exporter.setExporterInput(new SimpleExporterInput(print));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outFileName));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true);
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    } catch (JRException e) {
        e.printStackTrace();
    }
}

The bean class,

public class FundBean {

  private Double debit;
  private Double credit;

  public Double getCredit() {
      return credit;
  }

  public void setCredit(Double credit) {
      this.credit = credit;
  }

  public Double getDebit() {
      return debit;
  }

  public void setDebit(Double debit) {
      this.debit = debit;
  }
}

The beanFactory class which creates the list,

public class FundBeanFactory {

  public static List<FundBean> createBeanCollection(){
    List<FundBean> fundBeans   = new ArrayList<FundBean>();

    FundBean bean1 = new FundBean();
    bean1.setCredit(89201.12);
    bean1.setDebit(122392.23);

    FundBean bean2 = new FundBean();
    bean2.setCredit(95650.16);
    bean2.setDebit(787878.80);
    fundBeans.add(bean1);
    fundBeans.add(bean2);

    return fundBeans;
  }
 }

The jrxml file:

<parameter name="Credit" class="java.lang.Double"/>
<field name="debit" class="java.lang.Double"/>
<field name="credit" class="java.lang.Double"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch">
        <staticText>
            <reportElement mode="Opaque" x="61" y="17" width="420" height="43" backcolor="#999999" uuid="6878b8e7-ffdc-4465-842f-c1b6de0b5d87"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[              Available Funds Test]]></text>
        </staticText>
    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="151" splitType="Stretch">
        <staticText>
            <reportElement mode="Opaque" x="0" y="0" width="100" height="41" backcolor="#0033CC" uuid="2fca55e7-bfc3-4795-9735-5f4ca5b621e6"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[Debits]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="100" y="0" width="100" height="41" backcolor="#0066CC" uuid="25536a17-ca40-4256-bc82-fca3b79be2ab"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[Credits]]></text>
        </staticText>
        <textField>
            <reportElement x="0" y="41" width="100" height="67" uuid="56004fd8-48e8-4cfe-9ecc-53324b94f8a2"/>
            <textFieldExpression><![CDATA[$F{debit}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="100" y="41" width="100" height="67" uuid="ba4e4ab6-4a0f-4486-b2e6-15ffc0d04808"/>
            <textFieldExpression><![CDATA[$F{credit}]]></textFieldExpression>
        </textField>
    </band>
</columnHeader>

bean2 credit and debit values are not printed, why is this happening?


Solution

  • To output report rows you need to put your fields in the detail band of the report. You have used the column header band which is, naturally, only printed once.