Search code examples
jasper-reports

Floating text field below subreport not stretching to band height


I'm using JasperReports 6.0.0 to create a report which displays a table with 2 columns. The first column is a text field, the second column is a subreport followed by a text field. I want the text field to stretch so that it fits the remaining space below the subreport.

The strange thing is: if the subreport has only one element, the text field stretches correctly.

enter image description here

However, if the subreport has more than one element, the text field just disappears (I checked, it's not underneath the subreport!)

enter image description here

My understanding so far is that the text field is shown only when the subreport doesn't exceed the initial height I've set for it. In the example above, I've set the initial height of the subreport to 20px. The text field in the subreport has an height of 20px. So, when I'm printing only one text field in the subreport, the actual height of the subreport is equal to its initial height, that is 20px. However when I'm printing three text fields, the actual height is 60px which is greater than the initial height of 20px. That's when the text field below the subreport just disappears. For context, if I set that text field to not stretch, it is shown correctly below the subreport no matter how many elements it has, but i NEED it to stretch.

Here's the code of the report:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2023-08-23T22:29:24 -->
<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="monitoringBulletinRainSensors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="dc5d5b62-3121-47f6-bc9c-a0c86a6de260">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\<user>\\JaspersoftWorkspace\\MyReports\\"]]></defaultValueExpression>
    </parameter>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="text" class="java.lang.String"/>
    <detail>
        <band height="30" splitType="Prevent">
            <property name="local_mesure_unitheight" value="pixel"/>
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <textField isStretchWithOverflow="true">
                <reportElement key="" stretchType="RelativeToBandHeight" x="0" y="0" width="50" height="30" uuid="17091a86-28eb-4f4b-814e-2049fc13b754">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unitwidth" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="50" y="0" width="160" height="20" isPrintInFirstWholeBand="true" backcolor="#FFFFFF" uuid="d8d61ef2-ee12-437a-be7d-9514dc16438d">
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <property name="local_mesure_unitheight" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unitwidth" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test_child.jasper"]]></subreportExpression>
            </subreport>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToBandHeight" x="50" y="20" width="160" height="10" uuid="23a8ffa1-34a5-437d-9dc3-1e55a571b2f4"/>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA["This text field should stretch"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

And here's the code of the subreport:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2023-08-23T17:48:48 -->
<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="test_child" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6ee7b94c-7f06-43f8-a02a-461787c5be45">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="text" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="20" splitType="Prevent">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToBandHeight" x="0" y="0" width="160" height="20" uuid="bcfd27d0-fc93-42bd-82fb-74a8d95ed706">
                    <property name="local_mesure_unitheight" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unity" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="px"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

And finally here's the JSON datasource I'm using (changing the number of children to simulate the two cases):

{
    "text": "This is a very very very very very very very long text",
    "children": [
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        }
    ]
}

Any help is greatly appreciated. Thank you!


Solution

  • For what is worth. I'm using JasperSoftStudio 6.20.5 but with compatability mode to 6.0.0 as you can see:

    enter image description here

    Noticed that the TextField that's below subreport is using stretchType="RelativeToBandHeight" which is deprecated now.

    Changing it to ContainerBottom did the trick for me:

    enter image description here

    • Examples:

    3 subreport regs:

    enter image description here

    7 subreport regs:

    enter image description here