JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - Tabular Sample (version 4.6.0)


Shows how a table structure could be created using simple text elements and their border.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Creating Table-like Structures

Using PDF tags to create tables in generated PDF documents


top

Creating Table-like StructuresDocumented by Sanda Zaharia


Description / Goal
How to create tables using text elements and their border.

Since
0.6.3

Other Samples
/demo/samples/table
/demo/samples/list


Table-like Structures - Overview

Whenever data has to be organized in distinct rows and columns, a table structure should be created, or at least emulated, in order to display these data. Before the built-in table component being included in the JasperReports library, the most common way to represent tabular data was to create table-like structures based on particular styles and layout configuration of the report elements. For instance, if elements in a detail band are configured to share the same y-coordinate and the same height, they will result as perfectly aligned in a row at runtime. This row layout is then repeated for each iteration through the datasource, leading to a table-like structure organized in rows and columns.
To obtain excellent results with table-like structures, it's recommended to carefully set the related layout attributes and styles, in order to get elements perfectly aligned, horizontally and vertically. Styles and box elements may also be used to individualize cell borders, backgrounds, forecolors, cell paddings, alternative row colors, etc.
Attributes that should be carefully handled are:
  • x
  • y
  • width
  • height
  • style
The Tabular Sample

The TabularReport.jrxml in the sample shows how to create a table using the page header section to generate the table header, the detail section to generate the table rows, and the column footer section to generate column footers. If the JRXML file is open with a report design viewer (such as iReport or JasperStudio), one can see the elements being perfectly aligned, both horizontally and vertically.
Let's take a closer look at how the last three elements in the page header section were configured:
<pageHeader>
  <band height="30">
    ...
    <staticText>
      <reportElement mode="Opaque" x="300" y="0" width="255" height="15" backcolor="gray">
        <property name="net.sf.jasperreports.export.pdf.tag.th" value="full"/>
        <property name="net.sf.jasperreports.export.pdf.tag.colspan" value="2"/>
        <property name="net.sf.jasperreports.export.pdf.tag.tr" value="end"/>
      </reportElement>
      <box>
        <pen lineWidth="0.5" lineColor="black"/>
        <topPen lineWidth="1" lineColor="red"/>
        <rightPen lineWidth="1" lineColor="red"/>
      </box>
      <textElement textAlignment="Center">
        <font size="12" isBold="true"/>
      </textElement>
      <text><![CDATA[Header 4]] ></text>
    </staticText>
    <staticText>
      <reportElement mode="Opaque" x="300" y="15" width="155" height="15" backcolor="gray">
        <property name="net.sf.jasperreports.export.pdf.tag.tr" value="start"/>
        <property name="net.sf.jasperreports.export.pdf.tag.th" value="full"/>
      </reportElement>
      <box leftPadding="10">
        <pen lineColor="black"/>
        <topPen lineWidth="0.5"/>
        <leftPen lineWidth="0.5"/>
        <bottomPen lineWidth="0.5"/>
      </box>
      <textElement>
        <font isBold="true"/>
      </textElement>
      <text><![CDATA[Header 4.1]] ></text>
    </staticText>
    <staticText>
      <reportElement mode="Opaque" x="455" y="15" width="100" height="15" backcolor="gray">
        <property name="net.sf.jasperreports.export.pdf.tag.th" value="full"/>
        <property name="net.sf.jasperreports.export.pdf.tag.tr" value="end"/>
      </reportElement>
      <box rightPadding="10">
        <pen lineColor="black"/>
        <topPen lineWidth="0.5"/>
        <leftPen lineWidth="0.5"/>
        <bottomPen lineWidth="0.5"/>
        <rightPen lineWidth="1" lineColor="red"/>
      </box>
      <textElement textAlignment="Right">
        <font isBold="true"/>
      </textElement>
      <text><![CDATA[Header 4.2]] ></text>
    </staticText>
  </band>
</pageHeader>
Notice that the Header 4 element is only 15 pixels high, while the band height is 30. It means that there is left some unused space below this static text. It starts from y=0, x=300 and is 255 pixels wide.
The next element labeled Header 4.1 starts from y=15 and is 15 pixels high. Vertically, it fits perfectly in the unused space below the Header 4 element. But its width=155 pixels is not enough to fill the entire available horizontal space. So, we still have unused space in this layout design.
This space will be completely covered by the last element labeled Header 4.2. It starts from y=15, x=455, is 15 pixels high and 100 pixels wide, therefore it fits perfectly in the available space.
This way was emulated a table header with nested column headers. With a little bit more accurate design, one could emulate column group headers and other header structures with increased complexity.
It should also be noticed how the box elements were designed, piece by piece, to configure borders for table and header cells. The background color for all header cells is set to grey.

Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/table within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/table/build/reports directory.
Then the report will open in the JasperReports internal viewer.

top

Using PDF tags to create tables in generated PDF documents


Description / Goal
How to create tables in generated PDF documents using PDF tags.

Since
3.1.2


[Under Construction]



© 2001- Jaspersoft Corporation www.jaspersoft.com