Search code examples
jasper-reports

JasperReport: Unexpected White Space Between <jr:listContents> Elements


Hitting quite the roadblock on this one. I have a primary report which loads a subreport. Said subreport contains nested lists, of which the inner list is rendering some unexpected white space between elements. The white space is consistent in size, which almost seems as if the report is rendering a "ghost" element.

Relevant jrxml for reference:

<subDataset name="SurveyListDataSet">
  <property name="com.jaspersoft.studio.data.defaultdataadapter" />
  <queryString><![CDATA[]]></queryString>
  <field name="surveyDate" class="java.lang.String"/>
  <field name="surveyList" class="java.util.List"/>
</subDataset>
<subDataset name="SurveyDataSet">
  <property name="com.jaspersoft.studio.data.defaultdataadapter" />
  <queryString><![CDATA[]]></queryString>
  <field name="question" class="java.lang.String"/>
  <field name="response" class="java.lang.String"/>
</subDataset>

<field name="surveyDataList" class="java.util.List"/>

<detail>
  <band height="17" splitType="Stretch">
    <componentElement>
      <reportElement x="0" y="0" width="505" height="17" isRemoveLineWhenBlank="true" />
      <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" printOrder="Vertical">
        <datasetRun subDataset="SurveyListDataSet">
          <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{surveyDataList})]]></dataSourceExpression>
        </datasetRun>
        <jr:listContents height="17" width="505">
          <textField isStretchWithOverflow="true">
            <reportElement style="report_value_bold" x="110" y="0" width="265" height="15" />
            <textElement textAlignment="Left" />
            <textFieldExpression><![CDATA[$F{surveyDate}]]></textFieldExpression>
          </textField>
          <line>
            <reportElement style="line_light" x="105" y="16" width="400" height="1"/>
          </line>
          <componentElement>
            <reportElement x="0" y="0" width="505" height="0" isRemoveLineWhenBlank="true" positionType="Float"/>
            <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" printOrder="Vertical">
              <datasetRun subDataset="SurveyDataSet">
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{surveyList})]]></dataSourceExpression>
              </datasetRun>
              <jr:listContents height="34" width="505">
                <textField isStretchWithOverflow="true">
                  <reportElement style="report_value" x="125" y="17" width="265" height="15" positionType="Float" />
                  <textElement textAlignment="Left" />
                  <textFieldExpression><![CDATA[$F{question}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                  <reportElement style="report_value_bold" x="300" y="17" width="195" height="15" isRemoveLineWhenBlank="true" positionType="Float" />
                  <textElement textAlignment="Right" />
                  <textFieldExpression><![CDATA[$F{response}]]></textFieldExpression>
                </textField>
                <line>
                  <reportElement style="line_light" x="105" y="33" width="400" height="1" positionType="FixRelativeToBottom" />
                </line>
              </jr:listContents>
            </jr:list>
          </componentElement>
        </jr:listContents>
      </jr:list>
    </componentElement>
  </band>
</detail>

Example output of what I'm seeing: enter image description here

Expected: each Q/A appears right below the Q/A-line above it. There should not be any gaps between dates and the Q/A above it either.

I've tried tweaking various heights, postitionTypes, etc. to no avail. Feel like I'm probably missing something obvious but I've been staring at this too long. Appreciate any help!


Solution

  • Your innermost list reportElement is set to have a height of 0px while suddenly the listContents height is set to 34px but then only contains a TextElement of height 17px while the Detail band is set to 17px.

    The basic concept when working with Lists in Jasperreports is that every inner list must have vertical space allocated in the list it is contained in. i.e. your innermost list must have space allocated in the outer list, positioned relative to other elements in the outer list. If a TextElement is streched due to isStretchWithOverflow="true" elements in the same List that are set to positionType="Float" will "Float" down and the list will extend. If the a inner list extends, the outer list will automatically also extend. (and elements in the outer list below the inner list will Float down if set to positionType="Float".

    I've cleaned up your example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.19.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="nbsp" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="77c97c1b-90ba-40a8-a5c3-f88b8a48a585">
        <subDataset name="SurveyListDataSet" uuid="da0fc5b7-7621-4907-b2b7-e8fa130b1945">
            <queryString>
                <![CDATA[]]>
            </queryString>
            <field name="_THIS" class="java.lang.String"/>
        </subDataset>
        <subDataset name="SurveyDataSet" uuid="e7a6a451-fe26-427c-917d-59070a76a200">
            <queryString>
                <![CDATA[]]>
            </queryString>
            <field name="_THIS" class="java.lang.String"/>
        </subDataset>
        <detail>
            <band height="34" splitType="Stretch">
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <componentElement>
                    <reportElement x="0" y="0" width="505" height="34" uuid="0d598531-c684-4c2b-be01-9ced3fa81f81">
                        <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    </reportElement>
                    <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" printOrder="Vertical">
                        <datasetRun subDataset="SurveyListDataSet" uuid="7eeeb416-bd57-43dc-bc19-47481921f2a7">
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList(new String[] { "2022-01-01", "2022-02-01", "2022-03-01", "2022-04-01" }))]]></dataSourceExpression>
                        </datasetRun>
                        <jr:listContents height="34" width="505">
                            <textField textAdjust="StretchHeight">
                                <reportElement x="110" y="0" width="265" height="15" uuid="c0bfd9da-139d-41a9-9b91-34c4db12ebe9"/>
                                <textElement textAlignment="Left"/>
                                <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                            </textField>
                            <line>
                                <reportElement positionType="Float" x="105" y="16" width="400" height="1" uuid="d21b7ef6-f724-4ad9-a238-c68c28d486ab"/>
                            </line>
                            <componentElement>
                                <reportElement positionType="Float" x="0" y="17" width="505" height="17" uuid="54082666-799b-4117-92a5-255041315f7b">
                                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                                    <property name="com.jaspersoft.studio.unit.y" value="px"/>
                                </reportElement>
                                <jr:list printOrder="Vertical">
                                    <datasetRun subDataset="SurveyDataSet" uuid="f955dac7-55df-4fc3-8e30-cdeefe73a5be">
                                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList(new String[] { "Question 1? ", "Question 2?", "Question 3?", "Question 4?" }))]]></dataSourceExpression>
                                    </datasetRun>
                                    <jr:listContents height="17" width="505">
                                        <textField textAdjust="StretchHeight">
                                            <reportElement x="125" y="0" width="265" height="15" uuid="c1fb3fef-53d7-4e64-b05a-0478aed19226">
                                                <property name="com.jaspersoft.studio.unit.y" value="px"/>
                                            </reportElement>
                                            <textElement textAlignment="Left"/>
                                            <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                                        </textField>
                                        <textField textAdjust="StretchHeight">
                                            <reportElement x="300" y="0" width="195" height="15" isRemoveLineWhenBlank="true" uuid="4dd5da20-e70f-4122-92e9-fea714edb33a">
                                                <property name="com.jaspersoft.studio.unit.y" value="px"/>
                                            </reportElement>
                                            <textElement textAlignment="Right"/>
                                            <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                                        </textField>
                                        <line>
                                            <reportElement positionType="Float" x="105" y="16" width="400" height="1" uuid="b2e07c02-8deb-49fb-9a68-9f431a9f363d">
                                                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                                                <property name="com.jaspersoft.studio.unit.y" value="px"/>
                                            </reportElement>
                                        </line>
                                    </jr:listContents>
                                </jr:list>
                            </componentElement>
                        </jr:listContents>
                    </jr:list>
                </componentElement>
            </band>
        </detail>
    </jasperReport>