I can't get data into a table. I'm using the example in Chapter 14, except that I use a Collection instead of the reports data source. Calling JasperFillManager.fillReport()
with the Collection in a JRBeanCollectionDataSource
doesn't show any of the data, but it creates an empty page for each element of the Collection.
Following this example I've switched to sending in the data in a Parameter. This gives an empty table on page 2 of the report (the example itself works).
My code is below.
IntelliJ complains that the jrxml generated by JasperSoft Studio is invalid. It says that the element can't be where it is. Maybe that has something to do with it.
How do I get data into a Table in JasperReports?
Java
public class JasperReportsLineItemTableExample {
public static Logger logger = LoggerFactory.getLogger(JasperReportsLineItemTableExample.class);
public static void main(String[] args) {
String reportJrxmlFile = "LineItems_Table_A4.jrxml";
String outputFile = "LineItemsTableExample.pdf";
List<LineItem> items = LineItemsUtil.createItems(10);
logger.info(items.size() + " items");
JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(items);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("LineItemDataSource", itemsJRBean);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(reportJrxmlFile);
// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, itemsJRBean);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
try (OutputStream outputStream = new FileOutputStream(new File(outputFile))) {
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
System.out.println("File Generated");
} catch (IOException | JRException ex) {
ex.printStackTrace();
}
} catch (JRException ex) {
ex.printStackTrace();
}
}
}
JRXML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="LineItems_Table_A4" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="565ea393-c6d0-4128-8564-05701d3de374">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<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>
<subDataset name="Dataset1" uuid="57478514-4d8c-49d7-a6e5-8566d1ec7332">
<queryString>
<![CDATA[]]>
</queryString>
<field name="itemNumber" class="java.lang.String">
<fieldDescription><![CDATA[itemNumber]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="unitCost" class="java.lang.Float">
<fieldDescription><![CDATA[unitCost]]></fieldDescription>
</field>
<field name="totalCost" class="java.lang.Float">
<fieldDescription><![CDATA[totalCost]]></fieldDescription>
</field>
</subDataset>
<parameter name="LineItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="260" y="34" width="100" height="30" uuid="096a5061-5710-4597-9ce7-e4a7edbfe155"/>
<text><![CDATA[LineItems Table]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="357" splitType="Stretch">
<componentElement>
<reportElement positionType="FixRelativeToBottom" x="60" y="110" width="440" height="200" uuid="657ac0a8-31c5-4336-8e8d-fdec5dc92bd6">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</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="Dataset1" uuid="ac1740d4-8a9f-409d-979c-ad8788815312">
<dataSourceExpression><![CDATA[$P{LineItemDataSource} ]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="a4de9f35-c7a8-4d2d-8ea1-339144d7d3d4">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="7ea912f2-e802-4b7a-9ccb-3e7b3762f862"/>
<text><![CDATA[itemNumber]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="6e4662e4-9421-4328-8d00-766416414c6d"/>
<textFieldExpression><![CDATA[$F{itemNumber}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="d10a0d67-02ea-4153-a799-6b4dbdd69200">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="4ba9a6bc-e6ed-477e-ae2f-0d91b27cd42f"/>
<text><![CDATA[quantity]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="366e5229-7cb8-441c-9a2a-82535212f06c"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="266fdaed-4f71-43d5-8515-89604973dcd9">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="50" height="30" uuid="c8bbc714-10b6-4305-bf55-9feb87c32498"/>
<text><![CDATA[unitCost]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="9db777d5-428c-4387-8e9b-02aa4422407e"/>
<textFieldExpression><![CDATA[$F{unitCost}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="b70c951b-951d-443f-9c21-8acf8b025f64">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="b9b914be-ab50-4e2e-bf52-fca2a891a7de"/>
<text><![CDATA[total Cost]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="071819a9-ab97-41df-b2b7-b8e9783dfb4d"/>
<textFieldExpression><![CDATA[$F{totalCost}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>