I'm trying to display several tables in one Jasper Report and I use sub reports for this feature. But I'm stuck with displaying several sub reports. Actually, only one of them (the first) is displayed.
Here is my main report.jrxml:
<?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="multitable" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1c6dc1c9-67af-43d5-bf1e-9243557c8048">
<property name="net.sf.jasperreports.json.source" value="C:/jdd/projects/StackTrace/jasper/multitable.json"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["reports/hardware/"]]></defaultValueExpression></parameter>
<parameter name="title" class="java.lang.String"/>
<parameter name="period" class="java.lang.String"/>
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<detail>
<band height="120" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="652" y="0" width="100" height="50" uuid="67e1ca07-588a-4b2b-ae93-ecbdb544b01b"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{period}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="50" width="555" height="20" uuid="48a7bbe4-b8ce-4a0d-a6e1-6ddd288e5602"/>
<dataSourceExpression><![CDATA[$P{DS1}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "printersSubreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
<summary>
<band height="305" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="50" width="555" height="32" uuid="780a1f25-065e-494a-ba1d-86a33b464343"/>
<dataSourceExpression><![CDATA[$P{DS2}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subjectSubreport.jasper"]]></subreportExpression>
</subreport>
</band>
</summary>
</jasperReport>
Here is subjectSubreport.jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="multitable_summary" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2a869c20-1e7d-4baa-92d7-c8d555f1f344">
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="subject" class="java.lang.String"/>
<field name="zipCode" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="23">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="a9730039-8418-4e58-a2eb-0babe8d2a380"/>
<text><![CDATA[Subject]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="a49b18ef-63c3-42bb-ae81-8d7d72ac9086"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Subject]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="c1c70fed-d887-429f-a3b0-ed645517e41f"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ZipCode]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="401436b3-d9a1-40bd-8a1d-dfa3f162f842"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{subject}]]></textFieldExpression>
</textField>
<textField pattern="###0">
<reportElement x="100" y="0" width="100" height="20" uuid="4447d3b0-6d3e-4795-8b5f-6a09d620448b"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{zipCode}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
And here is printersSubreport.jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="multitable_subreport" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="5f89d251-66b1-4658-9af5-c057de138fb6">
<field name="statusDesc" class="java.lang.String"/>
<field name="lastUpdateTime" class="java.lang.Integer"/>
<field name="criticalCount" class="java.lang.String"/>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="bbb8857c-471c-4aca-a8fc-336d2748d7b9"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Description]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="3fde89c9-d9a3-4972-8e3b-b2e619a51601"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[UpdateTime]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="200" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="e82ece60-c92e-4d0b-8881-29d7d0d78e81"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Count]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="cdec861a-8b3b-413f-8b99-74d0e18ee19d"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{statusDesc}]]></textFieldExpression>
</textField>
<textField pattern="###0.##">
<reportElement x="100" y="0" width="100" height="20" uuid="9dfb9f78-74e5-4562-84fd-ebb87c3203ce"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{lastUpdateTime}]]></textFieldExpression>
</textField>
<textField pattern="¤ #,##0.00">
<reportElement x="200" y="0" width="100" height="20" uuid="923ff62d-ea1f-410e-94d3-fa7c574c001b"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{criticalCount}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
When I switch sub reports the first of them displays correctly, and the other one disappears. In JasperSoft Studio everything is fine, the problem starts when I export them to pdf/xls.
Actually, only one of them (the first) is displayed.. When I switch sub reports the first of them displays correctly, and the other one disappears
You are declaring:
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
The JRDatasource is consumable, which means that if you use it to fill one report it will be consumed hence for next report calling next()
on same datasource will return false.
What you are experiencing is what happens if you pass same (same Object) datasource for DS1
and DS2
, DS1
will be consumed filling first subreport and then since DS1 == DS2
DS2
will already be consumed when filling second subreport, second report will have no records (empty).
You need to make a clone DS1
, create a new Object and pass that as DS2
. The JsonDataSource
is theoretically also a JRRewindableDataSource
, so you could call moveFirst()
before filling second subreport, but this is not very easy from the jrxml file so I don't suggest trying to use this method.