Using the Table feature in iReports and Jasper and java bean data sources

14,063

The package name you mentioned in the jrxml is wrong. It is net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.

Share:
14,063
eljaydub
Author by

eljaydub

Updated on June 08, 2022

Comments

  • eljaydub
    eljaydub almost 2 years

    I've spend the better part of 12hours now trying to figure out how to get charts and tables to work in jasper. This post is about the tables (for my chart issues, which are likely the same problem, see here)

    I want to make a table with two columns using the Table tool in the iReports palette. I have a List (previously was an ArrayList but I'm trying a List now since one of the tutorials I watched used that instead and they didn't have a problem) of String inside of the list of ProtoReport data object. The ProtoReport List is the one I wrap in a JRBeanCollectionDataSource and pass to JasperFill. I want the tableData List to fill one of the columns of my table. When I set this up according to how my limited experience with Jasper makes me think I should, I get the following error:

    net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
    1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
                            <---------------------------------------------------->
    2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getOldValue())); //$JR_EXPR_ID=12$
                            <---------------------------------------------------->
    3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
                value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
                            <---------------------------------------------------->
    3 errors
    
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
    at org.reportprotojava.protosheet.Program.main(Program.java:134)
    

    Here is my 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="ReportPrototype.jrxml" pageWidth="595" pageHeight="842" columnWidth="495" leftMargin="57" rightMargin="43" topMargin="43" bottomMargin="43" uuid="10825c57-f953-4166-bf03-8ecabe8a8f47">
    <property name="ireport.zoom" value="0.75"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="128"/>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#EFF7FF"/>
        </conditionalStyle>
    </style>
    <style name="table 2">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 2_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 2_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 2_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#EFF7FF"/>
        </conditionalStyle>
    </style>
    <subDataset name="ChartData" uuid="fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee"/>
    <subDataset name="Table Dataset 1" uuid="70531f85-19bf-4bd7-b801-6ed08b189b34">
        <field name="tableData" class="java.util.List"/>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <field name="title" class="java.lang.String"/>
    <field name="logoLocation" class="java.lang.String"/>
    <field name="picLocation" class="java.lang.String"/>
    <field name="paragraphText" class="java.lang.String"/>
    <field name="tableData" class="java.util.List"/>
    <detail>
        <band height="756" splitType="Stretch">
            <textField isStretchWithOverflow="true" pattern="">
                <reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="38" y="42" width="378" height="45" forecolor="#000099"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="html">
                    <font size="26"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
            <image onErrorType="Icon">
                <reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="216" y="273" width="279" height="246"/>
                <imageExpression><![CDATA[$F{picLocation}]]></imageExpression>
            </image>
            <image onErrorType="Icon">
                <reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/>
                <imageExpression><![CDATA[$F{logoLocation}]]></imageExpression>
            </image>
            <xyLineChart>
                <chart>
                    <reportElement uuid="ae87fc13-b92e-4a2a-b218-d395343f6028" x="0" y="537" width="495" height="203"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <xyDataset>
                    <dataset>
                        <datasetRun subDataset="ChartData" uuid="de7fb84d-17ea-4e5e-82bf-2015e72e4982"/>
                    </dataset>
                </xyDataset>
                <linePlot>
                    <plot/>
                </linePlot>
            </xyLineChart>
            <textField>
                <reportElement uuid="565b981c-ca6f-4eab-ab3e-683b1e2b4d03" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="103" width="495" height="144" backcolor="#CCCCCC"/>
                <textElement markup="html">
                    <font size="6"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{paragraphText}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement uuid="8eb2d942-e4be-4b86-b409-9e1b91f6b4c4" key="table 2" style="table 2" x="13" y="273" width="180" height="246"/>
                <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="Table Dataset 1" uuid="40906b08-698d-4979-ae31-7f1689859954">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{tableData})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column uuid="f1282add-60a7-49d7-b66d-c8bd912dc161" width="90">
                        <jr:tableHeader style="table 2_TH" height="30"/>
                        <jr:detailCell style="table 2_TD" height="20">
                            <textField>
                                <reportElement uuid="5c95b1c4-bfb0-4364-83da-28d78e2d0555" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[""+$F{tableData}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="ec2f8c0c-e950-4b74-9d1e-851f68deedf3" width="90">
                        <jr:tableHeader style="table 2_TH" height="30"/>
                        <jr:detailCell style="table 2_TD" height="20"/>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    

    And here is the beginner of my ProtoReport class (getters, setters, etc cut off):

    public class ProtoReport {
    
    
    private String outputFileName;
    private String title;
    private String logoLocation;
    private String paragraphText;
    private List<String> tableData;
    private String picLocation;
    private int[][] graphData;  //TODO decide how to store chart data
    private ChartData chartData;
    private String path;
    
    
    
    
    //default constructor
    public ProtoReport() {
    
        // Initialize object fields
        outputFileName = "PrototypeReport";
        title = "Prototype Report";
        paragraphText = "Default text";
    
        tableData = null;
        chartData = new ChartData();
    
        //set path to working directory
        path = System.getProperty("user.dir");
    
        //default to assumed report location 
        //(ie same folder as .jrxml and .jasper files)
        logoLocation = path + "\\reports\\logo.jpg";
        picLocation = path + "\\reports\\pic.jpg";
    
    }
    
    //constructor
    public ProtoReport(String title, String logoLocation, 
                            String picLocation, ChartData chartData) {
    
            // Initialize object fields
            outputFileName = "PrototypeReport";
            this.title = title;
            paragraphText = "Default text";
    
            //tableData = new ArrayList<String>();
            tableData = null;
            this.chartData = chartData;
    
            //set path to working directory
            path = System.getProperty("user.dir");
    
            //picture locations
            this.logoLocation = logoLocation;
            this.picLocation = picLocation;
    
        }
    
    
    
    //Getters and Setters
    
    //Self referencing method for use in iReport field description
        public ProtoReport getMe()
         {
          return this;
         }
    

    And finally, the relevant parts of my Main:

    public static void main(String[] args) {
    
        List<ProtoReport> listOfReports = new ArrayList<ProtoReport>();
    
        ProtoReport protoReport1 = new ProtoReport();
        ProtoReport protoReport2 = new ProtoReport();
    
    //test table.. since nothing else will work...
        List<String> testTable = new ArrayList<String>();
        testTable.add("First entry test");
        testTable.add("Second entry test");
        protoReport1.setTableData(testTable);
    
        protoReport1.getTableData().add("First entry");
        protoReport1.getTableData().add("Second entry");
     listOfReports.add(protoReport1);
    
    //and wrap the ArrayList in a JRBeanCollectionDataSource
        JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports);
    
        //build the jasper report
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        HashMap<String, Object> hashMap = new HashMap<>();
        boolean reportCreated;
    
        try {
    
            jasperReport = JasperCompileManager.compileReport(jrxmlLocation);
            jasperPrint = JasperFillManager.fillReport(jasperReport, hashMap, beanBurritoWrap);
            JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
            reportCreated=true;
        }
        catch (JRException e) {
              e.printStackTrace();
              reportCreated=false;
        }
    

    Since this is a similar problem to the one I'm having with my chart I suspect they have the same cause. I've been reading everything I can about how to use these features but somethings just not clicking... How to I set up the connections in iReport to fill my table with the strings in tableData? Do I need a list of strings for each column or is there a smarter way to do all this (keeping in mind I want to do it all with POJO's, ie with no sql database).

  • eljaydub
    eljaydub over 11 years
    You're right about that, that's from the autofill in iReports -when you select Use datasource expression under Connection/Datasource exp in the details pane, the default expression from iReports is: 'new net.sf.jasperreports.engine.JREmptyDataSource(1)' and I changed the JREmpty to JRBeanCollectionDataSource but did not notice the .data is missing. Is this a bug in iReports 4.7.0? I'll post again in a bit to verify this solved my problem
  • eljaydub
    eljaydub over 11 years
    Update: This fix corrected my compile time error. Then I had to re-arrange my data types because I could not get the List<String> of tableData to print the strings directly into a column while also using tableData as my data input source. I suspected this but wanted to see if I could get away with it. In the end I wrote a TableData class that stored a List<String> for each column and then put the column names as my fields under my Table Data source and then into the ireports table.