Senthil Krishnan
Senthil Krishnan

Reputation: 41

How to make all rows in table component stretch to be of same height?

I'm working with Jaspersoft iReport designer 4.7.0.I want to create a table with 3 columns and each cell in the table needs to be stretched to the largest object of the datasource.

I have tried with changing the properties of text element Position-float;stretch with overflow - true,Stretch type - relative to tallest object but it works for one row only.

I want this to effective for all rows of the table.

Current ouput

CurrentOutput

Expected output

expectedOutputwithEachCellSpacing

**jrxml used*

   <?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="report3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f32dfce1-c0d4-4f20-b896-653e7497d7ba">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <style name="table">
            <box>
                <pen lineWidth="1.0" lineColor="#000000"/>
            </box>
        </style>
        <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <subDataset name="tabeDataSet" uuid="46e73a3d-e883-4c74-a062-6984cc6dac58">
            <field name="id" class="java.lang.String"/>
            <field name="city" class="java.lang.String"/>
            <field name="street" class="java.lang.String"/>
        </subDataset>
        <subDataset name="Table Dataset 1" uuid="4a76bbab-d785-49f2-82aa-705ea0a2e0d1"/>
        <subDataset name="Table Dataset 2" uuid="984926ca-9b74-4713-a4a8-25e7e3a32697"/>
        <parameter name="tableData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
        <detail>
            <band height="191">
                <componentElement>
                    <reportElement uuid="6e73c624-495f-4558-bfe4-65a0487825ab" key="table 1" x="74" y="49" width="360" height="52"/>
                    <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="tabeDataSet" uuid="dcaeff11-66ee-47b5-af06-d2f183cb5830">
                            <dataSourceExpression><![CDATA[$P{tableData}]]></dataSourceExpression>
                        </datasetRun>
                        <jr:column uuid="ad308669-ebfb-43ca-b518-326779bbb20e" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="34fe3944-c103-4c85-b509-22df1b589d11" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column uuid="0dbf249d-ee6e-4431-bba2-6d13b610582f" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="886ba9a8-98bc-43cf-9abd-c6fd0410029a" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column uuid="edbd158e-278a-4a5d-98ab-4ea592001ee9" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="59ec1fc2-0a76-4c4c-96b0-7e2b5fbad3df" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{street}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
        </detail>
    </jasperReport>

Upvotes: 1

Views: 3304

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

No

there is no attribute that you can set to achieve that all rows will have same size, when one is stretching.

AFIK upto version ‎6.7.0 date of this answer

Naturally you can manuale increase height in jrxml file but if you like to do it dynamically, you need to do something as:

  1. Calculate the height you need looping you dataset in java finding the maximum height of the text using font metrics see this answer to understand how font metrics can be used.

  2. Load your jrxml as a JasperDesign object and then set the new height.

Crude example, considering that I have set id on my table component that is present in summary band and each cell has exactly 1 textField.

int newHeight = 40;
//Load into desgin  
JasperDesign design = JRXmlLoader.load("path/to/my/report.jrxml");
JRDesignBand summary = (JRDesignBand) design.getSummary();
//I have set id on my component in jrxml so that I find it quickly
JRDesignComponentElement tblElement = (JRDesignComponentElement) summary.getElementByKey("tbl");
StandardTable tbl = (StandardTable) tblElement.getComponent();
//loop all columns and set new height to both cell and textField
for (BaseColumn bc : tbl.getColumns()) {
    StandardColumn col = (StandardColumn) bc;
    DesignCell cell = (DesignCell) col.getDetailCell();
    JRDesignTextField tf = (JRDesignTextField) cell.getChildren().get(0);
    cell.setHeight(newHeight);
    tf.setHeight(newHeight);
}
//Get my report from design and later fill it.
JasperReport report = JasperCompileManager.compileReport(design);

Upvotes: 1

Related Questions