Jasper Reports - align dynamic text fields and their labels horizontally
Asked Answered
E

4

21

I'm using Jasper report 5.2, iReport 5.2 and exporting the report in RTF and PDF formats.

In my report I want to add few text fields along with their (static text)labels aligned horizontally like

         Name:  $F{name}
          Age:  $F{age}
Date of Birth:  $F{dateOfBirth}

But I'm unable to align them. This is what I tried

  • Position Type: float (for all static text and fields)
  • Stretch Type: no stretch (for all static text and fields)
  • Stretch With Overflow: True (for all dynamic text fields)

The image shows what I get and what I want. Moreover, my text field's content is dynamic i.e. content size could vary. enter image description here

I've read many forums but could not find a solution, please suggest.

Thanks

Evincive answered 3/9, 2013 at 19:15 Comment(0)
T
36

It can be easily done with help of container - the Frame element.

You should put Frame with Position Type as Float and put to them both staticText (label) and textField.

For textField I've set Position Type as Float the Stretch With Overlfow as true.

The sample

The jrxml file:

<?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="textfields_allign" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d855bf18-5e9b-4060-8caa-3fdd08abce3b">
    <parameter name="name" class="java.lang.String"/>
    <parameter name="id" class="java.lang.String"/>
    <parameter name="date" class="java.lang.String"/>
    <title>
        <band height="69" splitType="Stretch">
            <frame>
                <reportElement uuid="314bfd5b-7b0a-42f4-aca3-e61f0283f126" positionType="Float" x="213" y="1" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="e07201bb-a677-4dc9-a332-f94e6eca2722" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="66c93a44-9015-4ae9-bf05-b68b2420f3ef" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
                </textField>
            </frame>
            <frame>
                <reportElement uuid="8311a483-955e-4280-a9ac-513d7d34495b" positionType="Float" x="213" y="21" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="da404b4e-7908-40ae-8e1b-38e19d9ddc7f" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[ID]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="e0e312e8-cd2f-48af-8ae8-df95c6195488" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{id}]]></textFieldExpression>
                </textField>
            </frame>
            <frame>
                <reportElement uuid="8ff2baf9-b0ec-4c8a-b54a-9edd08b200cc" positionType="Float" x="213" y="41" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="b8d97db0-250a-43c8-a2f3-2fa3668c5d15" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[Date]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="4daa4d17-28be-4ac8-8e1e-2efbeec5f690" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </title>
</jasperReport>

The report's design in iReport:

enter image description here

The result will be (via iReport preview):

enter image description here

Tocharian answered 4/9, 2013 at 8:36 Comment(0)
C
4

To complete Alex K's solution : with Jaspersoft Studio, do not forget to set the property of the Static Text "stretch type" to "relative to tallest object".

Checkers answered 21/4, 2016 at 14:18 Comment(1)
and if you want skip whole frame if value column is empty - don't forget to set flag "Remove Line When Blank" and fill "Print When Expression" at frame propertiesUngley
V
3

In Jasper Reports 3.6 and later (including 5.2), there is a facility to use multiple details bands. You can put the first two blocks of Name and the text of "Sally Admison ..." in the first band. Below it would be your Patient ID and the "1234567890Blah..." and below that would be another details band with Date of Birth. This will give you the results that you want.

Do not refer to my post here as this is for the older 3.5 version of Jasper Reports. That version does not allow you to use multiple bands and I had to live with one single details band. This is not the case with 5.2. You can have fun with multiple details bands. Enjoy!

Vann answered 4/9, 2013 at 7:29 Comment(1)
Though this is not very neat but this solution works and you got me thinking in a different way. For everyone who may face the same problem, the above solution by @Vann works fineEvincive
B
0

Just increase the width of your details pane, that sorted the alignment issues for me.

Bed answered 11/9, 2014 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.