Showing posts with label jasper-reports. Show all posts

How to fill Jasper Table using Collection of data using Java?

Following excerpt show how you can fill the Jasper Table using Collection of data in Java. Watch the video to understand the steps easily.



Source code (template_Table.jrxml)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1  -->
<!-- 2015-11-25T00:02:30 -->
<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="template_Table" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1a0aa80b-dc07-4dca-bdbc-aa78eeb0d12b">
    <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="ItemDataset" uuid="2914f7c6-c2d7-448b-b0c1-090970e18ed6">
        <queryString>
   <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="price" class="java.lang.Double"/>
    </subDataset>
    <parameter name="ItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
  <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="256" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="200" uuid="7786bb05-503a-4b6a-991a-a7bcf3abad07">
                    <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="ItemDataset" uuid="d3e1e03a-2c83-4436-9e50-79518bc3d337">
                        <dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="130" uuid="dca8961f-ce2d-47ac-879c-b15847d170a9">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <jr:tableHeader height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="130" height="30" uuid="f60d1675-6fb6-4569-93ca-de32b3a8e861"/>
                                <text><![CDATA[Item Name]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="30">
                            <textField>
                                <reportElement x="0" y="0" width="130" height="30" uuid="f1de366a-b36f-4200-9b1e-7a0009be5373"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="140" uuid="980fffb5-1088-47a6-b00f-b5d08b7093c9">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                        <jr:tableHeader height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="140" height="30" uuid="16679b95-2e6e-4ca1-b8b6-dd7b74267c56"/>
                                <text><![CDATA[Item Price]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="30">
                            <textField>
                                <reportElement x="0" y="0" width="140" height="30" uuid="341e7faf-9680-4941-a9a4-ac78b8f4aae0"/>
                                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

Source code (Item.java)
public class Item {

    private String name;
    private Double price;

    // getter-setter
}

Source code (JasperTableExample.java)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.javaquery.bean.Item;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * @author javaQuery
 * @date 24nd November, 2015
 * @Github: https://github.com/javaquery/Examples
 */
public class JasperTableExample {

    public static void main(String[] args) {
        try {
            /* User home directory location */
            String userHomeDirectory = System.getProperty("user.home");
            /* Output file location */
            String outputFile = userHomeDirectory + File.separatorChar + "JasperTableExample.pdf";

            /* List to hold Items */
            List<Item> listItems = new ArrayList<Item>();

            /* Create Items */
            Item iPhone = new Item();
            iPhone.setName("iPhone 6S");
            iPhone.setPrice(65000.00);

            Item iPad = new Item();
            iPad.setName("iPad Pro");
            iPad.setPrice(70000.00);

            /* Add Items to List */
            listItems.add(iPhone);
            listItems.add(iPad);

            /* Convert List to JRBeanCollectionDataSource */
            JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(listItems);

            /* Map to hold Jasper report Parameters */
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("ItemDataSource", itemsJRBean);

            /* Using compiled version(.jasper) of Jasper report to generate PDF */
            JasperPrint jasperPrint = JasperFillManager.fillReport("resources/com/javaquery/jasper/templates/template_Table.jasper", parameters, new JREmptyDataSource());

            /* outputStream to create PDF */
            OutputStream outputStream = new FileOutputStream(new File(outputFile));
            /* Write content to PDF file */
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

            System.out.println("File Generated");
        } catch (JRException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

How to make bold font in jasper report pdf?

<textElement> tag along with <font> tag used to make staticText or textField content bold. Add following textElement block between starting tag of <staticText> or <textField> and ending tag of </staticText> or </textField> .
<textElement markup="styled">
    <font size="20" isBold="true" pdfFontName="Helvetica-bold"/>
</textElement>
Will look like...
<staticText>
    <reportElement x="235" y="0" width="92" height="26" uuid="3ed74d79-d31c-42ce-9af1-20ef78e0f6a1">
        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
    </reportElement>
    <textElement markup="styled">
        <font size="20" isBold="true" pdfFontName="Helvetica-bold"/>
    </textElement>
    <text><![CDATA[Bold Title]]></text>
</staticText>
Note: <font> tag is deprecated and replaced by Font Extensions. Will write post on how to use Font Extensions to make font bold.

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: .

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: .
 at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:872)
 at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:166)
 at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:417)
 at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:150)
 at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:330)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:382)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:357)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2031)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
 at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)
 at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
 at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:583)
 at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:929)
 at com.org.openxcell.channel.ftp.processor.InvoiceProcessor.createStandardPackage(InvoiceProcessor.java:643)
 at com.org.openxcell.channel.ftp.processor.InvoiceProcessor.main(InvoiceProcessor.java:676)
Caused by: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: .
 at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:132)
 at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:97)
 at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:108)
 at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1373)
 at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1274)
 at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1250)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1003)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:120)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
 at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:736)
 at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:58)
 at org.apache.commons.javaflow.bytecode.StackRecorder.execute(StackRecorder.java:95)
 at org.apache.commons.javaflow.Continuation.continueWith(Continuation.java:170)
 at org.apache.commons.javaflow.Continuation.startWith(Continuation.java:129)
 at org.apache.commons.javaflow.Continuation.startWith(Continuation.java:102)
 at net.sf.jasperreports.engine.fill.JRContinuationSubreportRunner.start(JRContinuationSubreportRunner.java:52)
 at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:847)
 ... 17 more
Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class com.org.openxcell.channel.ftp.processor.SalesOrderItemDataSource'
 at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
 at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
 at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
 at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
 at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:112)
 ... 33 more

I got above exception because I've table with subDataset containing <field> tag along with <fieldDescription> tag in it.
<field name="firstname" class="java.lang.String">
 <fieldDescription><![CDATA[]]></fieldDescription>
</field>
Solution
Remove <fieldDescription> tag from <field> tag.
<field name="firstname" class="java.lang.String"/>

net.sf.jasperreports.engine.JRRuntimeException: java.lang.StackOverflowError

I got following exception when one of my table in Jasper report grows to next page.
log4j:WARN No appenders could be found for logger (net.sf.jasperreports.engine.fill.BaseReportFiller).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
net.sf.jasperreports.engine.JRRuntimeException: java.lang.StackOverflowError
 at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:872)
 at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:166)
 at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:417)
 at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:150)
 at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:330)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:382)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:357)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2031)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
 at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)
 at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
 at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:583)
 at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:929)
Caused by: java.lang.StackOverflowError
 at java.util.HashMap$EntryIterator.<init>(Unknown Source)
 at java.util.HashMap$EntrySet.iterator(Unknown Source)
 at java.util.AbstractMap.hashCode(Unknown Source)
 at java.util.Objects.hashCode(Unknown Source)
 at java.util.HashMap$Node.hashCode(Unknown Source)
 ...
net.sf.jasperreports.engine.JRException: The report filling thread was interrupted.
 at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:126)
 at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:583)
 at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:929)
 ...
Caused by: net.sf.jasperreports.engine.fill.JRFillInterruptedException: Report filling process interrupted.
 at net.sf.jasperreports.engine.fill.BaseReportFiller.checkInterrupted(BaseReportFiller.java:535)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:369)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:357)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBackground(JRVerticalFiller.java:1819)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:252)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
 at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)
 at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
 ... 4 more
net.sf.jasperreports.engine.JRRuntimeException: java.lang.StackOverflowError
 at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:872)
 at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:166)
 at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:417)
 at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:150)
 at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:330)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:382)
 at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:357)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2031)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
 at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
 at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
 at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)
 at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
 at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:583)
 at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:929)
 ...
Caused by: java.lang.StackOverflowError
 at java.util.AbstractMap.hashCode(Unknown Source)
 at java.util.Objects.hashCode(Unknown Source)
 at java.util.HashMap$Node.hashCode(Unknown Source)
 ...

Jasper Exception StackOverflowError

Solution 1
Remove org.apache.log4j jar from your build path and I guess its nearly impossible for you to do so, as you used this log4j everywhere in your code. Solution 2 will save you.

Solution 2
Move your log4j.properties file in src folder. This will work.

Reference
http://community.jaspersoft.com/jasperreports-library/issues/5015#comment-709695