Search code examples
jasper-reportsjavabeans

Jasper report - the first line from a list is not displayed


I use Jasper reports 6.17 and I made a list in Jasper Studio.

The list has only 2 items named "test1" and "test2" but only one is displayed and I don't know why. The resulting PDF displays only "test2", why is "test1" missing? If I add 10 items the first line will be missing.

The jrxml file is:

<?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="LIST" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e7ee8129-93f1-4ca4-ba39-4b3d41bc1dd1">
    <subDataset name="Item" uuid="e9567b90-2de8-4e93-bad6-f67f0139e348">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement x="178" y="31" width="100" height="30" uuid="3c38bb5e-a468-491b-8a3c-e04104fd93fe"/>
                <jr:list 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="Item" uuid="16e7dfb2-6696-4eb8-98a9-c590e4c9fc28">
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="30" width="100">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="2ff6723b-b461-420c-b233-ac3963d3b41b"/>
                            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

And the java files are:

public class JasperTestList2 {
    public static void main(String[] args) {
        try {
            List<Item> its = new ArrayList<>();
            
            Item i1 = new Item();
            i1.setName("test1");
            its.add(i1);
            
            Item i2 = new Item();
            i2.setName("test2");
            its.add(i2);
            
            JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(its);

            JasperPrint jasperPrint = JasperFillManager.fillReport("JasperReports/LIST.jasper", null, itemsJRBean);

            OutputStream outputStream = new FileOutputStream(new File("JasperList.pdf"));
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        } catch (JRException | FileNotFoundException ex) {
            Logger.getLogger(JasperTestList2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public class Item {
    private String name;
    public void setName(String name){this.name = name;}
    public String getName()         {return name;}
}

Solution

  • This isn't a great answer, I get it, but I can't format it in comments.

    1. What you're doing.
    datasource = [
       Item1,  <-- detail band next()
       Item2   <-- list datasource next()
    ]
    
    1. What you're doing now.
    parameter(item) = [
       Item1,  <-- list datasource next()
       Item2   <-- list datasource next()
    ],
    datasource = 
    
    1. What you probably want to do.
    datasource = [
       items = [  <-- main datasource next()
           item1, <-- list datasource next()
           item2  <-- list datasource next()
    ]
    

    Obviously you need to work out the objects for your JRBeanCollectionDatasource but you want them to look like that.

    public class ReportItem {
       private List<String> items;
    }
    

    Then building out a List<ReportItem> that you pass into the Datasource would work.