Java – How to show/hide a column at runtime

jasper-reportsjava

I'd like to show/hide a column at runtime based on a particular condition. I'm using "Print when expression" to conditionally show/hide this column (and it's header) in my report. When the column is hidden, the space it would have occupied is left blank, which is not particularly attractive.

I would prefer if the extra space was used in a more effective manner, possibilities include:

  • the width of the report is reduced by the width of the hidden column
  • the extra space is distributed among the remaining columns

In theory, I could achieve the first by setting the width of the column (and header) to 0, but also indicate that the column should resize to fit its contents. But JasperReports does not provide a "resize width to fit contents" option.

Another possibility is to generate reports using the Jasper API instead of defining the report template in XML. But that seems like a lot of effort for such a simple requirement.

Best Solution

In later version (v5 or above) of jasper reports you can use the jr:table component and truly achieve this (without the use of java code as using dynamic-jasper or dynamic-reports).

The method is using a <printWhenExpression/> under the <jr:column/>

Example

Sample Data

+----------------+--------+
|      User      |  Rep   |
+----------------+--------+
| Jon Skeet      | 854503 |
| Darin Dimitrov | 652133 |
| BalusC         | 639753 |
| Hans Passant   | 616871 |
| Me             |   6487 |
+----------------+--------+

Sample jrxml

Note: the parameter $P{displayRecordNumber} and the <printWhenExpression> under first jr:column

<?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="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
    <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="tableDataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">
        <field name="User" class="java.lang.String"/>
        <field name="Rep" class="java.math.BigDecimal"/>
    </subDataset>
    <parameter name="displayRecordNumber" class="java.lang.Boolean">
        <defaultValueExpression><![CDATA[true]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="50">
            <componentElement>
                <reportElement key="table" style="table" x="0" y="0" width="555" height="47" uuid="76ab08c6-e757-4785-a43d-b65ad4ab1dd5"/>
                <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="tableDataset" uuid="07e5f1c2-af7f-4373-b653-c127c47c9fa4">
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="90" uuid="918270fe-25c8-4a9b-a872-91299cddbc31">
                        <printWhenExpression><![CDATA[$P{displayRecordNumber}]]></printWhenExpression>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="5cd6da41-01d5-4f74-99c2-06784f891d1e"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <text><![CDATA[Record number]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="30" uuid="5fe48359-0e7e-44b2-93ac-f55404189832"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="7979d8a2-4e3c-42a7-9ff9-86f8e0b164bc">
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="61d5f1b6-7677-4511-a10c-1fb8a56a4b2a"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <text><![CDATA[Username]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="30" uuid="a3cdb99d-3bf6-4c66-b50c-259b9aabfaef"/>
                                <box leftPadding="3" rightPadding="3"/>
                                <textElement verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="625e4e5e-5057-4eab-b4a9-c5b22844d25c">
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="e1c07cb8-a44c-4a8d-8566-5c86d6671282"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <text><![CDATA[Reputation]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField pattern="#,##0">
                                <reportElement x="0" y="0" width="90" height="30" uuid="6be2d79f-be82-4c7b-afd9-0039fb8b3189"/>
                                <box leftPadding="3" rightPadding="3"/>
                                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{Rep}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </title>
</jasperReport>

Output with $P{displayRecordNumber}=true

true

Output with $P{displayRecordNumber}=false

false

As you can see the columns adapts nicely on the basis of which are displayed.