Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
JasperReports 3.5 for Java Developers

You're reading from  JasperReports 3.5 for Java Developers

Product type Book
Published in Aug 2009
Publisher Packt
ISBN-13 9781847198082
Pages 368 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

JasperReports 3.5 for Java Developers
Credits
About the Author
About the Reviewers
Preface
1. An Overview of JasperReports 2. Adding Reporting Capabilities to our Java Applications 3. Creating your First Report 4. Creating Dynamic Reports from Databases 5. Working with Other Datasources 6. Report Layout and Design 7. Adding Charts and Graphics to Reports 8. Other JasperReports Features 9. Exporting to Other Formats 10. Graphical Report Design with iReport 11. Integrating JasperReportswith Other Frameworks Index

Chapter 6. Report Layout and Design

All reports we have created so far contain simple layouts. In this chapter, we will cover how to create elaborate layouts, including, among other JasperReports features, adding background images or text to a report, logically grouping report data, conditionally printing report data, and creating subreports.

In this chapter, we will cover the following topics:

  • How to control report-wide layout properties

  • How to use styles to control the look of report elements

  • How to add multiple columns to a report

  • How to divide report data into logical groups

  • How to allow report text fields to stretch for displaying large amounts of data

  • How to control the layout of the report elements, including how to control their position, width, and height, among other layout properties

  • How to use the <frame> element to visually group report elements

  • How to add background text to a report

  • How to add dynamic data to a report through report expressions and variables

  • How to conditionally...

Controlling report-wide layout properties


The <jasperReport> root element of the JRXML template contains a number of attributes that allow us to control report layout. The following table summarizes these attributes:

Setting text properties


JasperReports provides several ways to control text properties in the report. We can control the font, whether the text is bold, italic, underlined, its background and foreground colors, and so on.

Styles

One way JasperReports allows us to control text properties is by using the <style> element. This element allows us to control the foreground and background colors, the style of font (bold, italic, or normal), the font size, a border for the font, and many other attributes. Styles can extend other styles and add to or override properties of the parent style.

The following JRXML template illustrates the use of styles:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReport name="ReportStylesDemo"
              xmlns="http://jasperreports.sourceforge.net/jasperreports"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://jasperreports
                        .sourceforge.net/jasperreports http...

Setting a report's background


Reports can have elements that appear in the report background, behind all other report elements. We can add any report element to the background by using the JRXML <background> element. The following JRXML template demonstrates how to do this:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="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="BackgroundDemoReport" >
  <style name="centeredText" hAlign="Center" vAlign="Middle" />
  <style name="boldCentered" style="centeredText" isBold="true" />
  <style name="backgroundStyle" style="boldCentered"
         fontName="Helvetica" pdfFontName="Helvetica-Bold"
         forecolor="lightGray" fontSize="90"/>
  <background>
    <band height...

Report expressions


Using report expressions, another feature of JasperReports, we can display calculated data on a report. Calculated data is the data that is not static and not specifically passed as a report parameter or a datasource field.

Report expressions are built from combining report parameters, fields, and static data. By default, report expressions can be built using the Java language, but JasperReports can support any other language supported by the JVM. The JasperReports project file includes examples of using BeanShell and Groovy to build report expressions.

Note

By far the most commonly used report expressions are Java expressions, so we will cover only those. Refer to the examples distributed with the JasperReports project ZIP file if you need to create expressions in BeanShell or Groovy.

We have already seen simple report expressions in the form of report parameters and fields. We can use any valid Java language expression that returns a string or a numeric value as report expressions...

Adding multiple columns to a report


JasperReports allows us to generate reports with multiple columns. Reports we have seen so far seem to have multiple columns. For example, the report we created in the previous section has a column for model, another column for tail number, and one more for serial number. However, all three of these fields are laid out in a single <band> element.

When we add multiple columns to a report, we should think of the data inside a band as a cell, regardless of how the data is laid out inside that band.

The f lightstats database we used for the examples in Chapter 4, Creating Dynamic Reports from Databases, contains the country, state, and city where an aircraft is registered. Let's create a report displaying the tail number of all aircraft registered in the state of New York in the United States. Our report will display the data in three columns. The following JRXML template will generate a report with the desired layout:

<?xml version="1.0" encoding="UTF...

Grouping report data


JasperReports allows us to group report data in a logical manner. For example, if we were creating a report about cars, we could group the data by car make and/or model. If we were creating a report about sales figures, we could group the report data by geographical area.

The flightstats database we used for the examples in Chapter 4, Creating Dynamic Reports from Databases, contains the country, state, and city where an aircraft is registered. Let's create a report displaying aircraft data registered in any state starting with the letter "A" in the United States. We will group the report data by state abbreviation. The JRXML template for the report is as follows:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="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...

Report variables


When we wrote the report in the Report Expressions section, we had to type the following expression twice:

$F{fixed_wing_single_engine_cnt}.intValue() +$F{fixed_wing_multiple_engine_cnt}.intValue())

This expression was typed once to calculate the number of fixed-wing aircraft reported, and again to calculate the total number of aircraft reported. This duplication is not a good thing because, if we need to change the expression for any reason, we would have to do it twice. JasperReports allows us to assign report expressions to a variable, eliminating the need to type the expression multiple times. The following JRXML template is a modified version of the one we wrote in that section, this version takes advantage of report variables to eliminate the duplicate expression.

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation...

Stretching text fields to accommodate data


By default, <textField> elements have a fixed size. If the data they need to display does not fit in their defined size, it is simply not displayed in the report. This is rarely the behavior we would want. Luckily, JasperReports allows us to alter this default behavior. This is accomplished by setting the isStretchWithOverflow attribute of the <textField> element to true.

The following JRXML template demonstrates how to allow text fields to stretch so that they can accommodate large amounts of data:

<?xml version="1.0" encoding="UTF-8" ?>
<jasperReportxmlns="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="TextFieldStretchDemo">
  <field name="lots_of_data" class="java.lang.String" />
 ...

Laying out report elements


As we saw in Chapter 3, Creating your First Report, a report can contain the following sections: a report title, a page header, a page footer, a column header, a column footer, a detail section, a report summary, and a last page footer. These sections are defined by the <title>, <pageHeader>, <pageFooter>, <columnHeader>, <columnFooter>, <detail>, <summary>, and <lastPageFooter> JRXML elements, respectively.

Each of these elements contains a single <band> element as its only subelement. The <band> element can contain zero or more <line>, <rectangle>, <ellipse>, <image>, <staticText>, <textField>, <subReport>, or <elementGroup> subelements. Except for <elementGroup>, each of these elements must contain a single <reportElement> as its first element. The <reportElement> subelement determines how data is laid out for that particular element...

Setting common element properties


JasperReports 1.1 introduced a new report element, the <frame> element, allowing us to group elements together and give them a common look. For example, we can set the background color of the frame, and it will be inherited across all the elements contained within the frame. Frames also provide a straightforward way of placing a border around multiple report elements. The following JRXML template is a new version of the example in the previous section. It has been modified to illustrate the use of frames.

<?xml version="1.0" encoding="UTF-8" ?>
<jasperReportxmlns="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="FrameDemo">
  <field name="lots_of_data" class="java.lang.String" />
  <...

Hiding repeated values


Sometimes report elements can have the same value over and over. For example, the report we created in the Grouping reportdata section is sorted by model number. For airplanes that are the same model, we see the model number repeated over and over again. Perhaps the report would be easier to read if we printed the model number only when it is different from the previous one. This will add to report readability, as the person reading the report would not have to look at the model number unless it is different from the previous one. In the following example, we will modify the JRXML template discussed in that section to accomplish this:

<?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...

Subreports


One nice feature of JasperReports is that it allows incorporating a report within another report. That is, one report can be a subreport of another. Subreports allow us to keep report designs simple, as we can create many simple reports and encapsulate them into a master report.

Let's create a more detailed version of the report discussed in the previous section. This new version divides the reported aircraft into the city to which they are registered. We will create one report that displays the aircraft registered in each city for a particular state. Then, we'll use that report as a subreport for a master report that divides the aircraft by state. The JRXML template for the subreport is as follows:

<?xml version="1.0" encoding="UTF-8"  ?>
<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://jasperreports.sourceforge
                  .net/jasperreports...

Summary


In this chapter, we discussed several JasperReports features that allow us to control the layout of a report.

By setting the appropriate attributes of the <jasperReport> JRXML element, we can control report-wide layout properties, such as margins, page width, page orientation, and others. Text properties such as size and font can be set by using report styles or by setting the appropriate attributes of the <text> JRXML element. We can use styled text to modify the style of individual words, phrases, or sentences by setting the isStyledText attribute of the <textElement> JRXML element.

We learned to add multiple columns to a report by setting the columnCount attribute of the <jasperReport> JRXML element. We also learned to divide the report data into logical groups by using the <group> JRXML element. The chapter also dealt with displaying dynamic data in a report by using report expressions, encapsulating report expressions using report variables, and performing...

lock icon The rest of the chapter is locked
You have been reading a chapter from
JasperReports 3.5 for Java Developers
Published in: Aug 2009 Publisher: Packt ISBN-13: 9781847198082
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}

Attribute

Description

Valid values

Default values

pageWidth

Determines the width of the page in pixels.

Any non-negative integer.

595

pageHeight

Determines the height of the page in pixels.

Any non-negative integer.

842

leftMargin

Determines the left margin of the page in pixels.

Any non-negative integer.

20

rightMargin

Determines the right margin of the page in pixels.

Any non-negative integer.

20

topMargin

Determines the top margin of the page in pixels.

Any non-negative integer.

30

bottomMargin

Determines the bottom margin of the page in pixels.

Any non-negative integer.

30

orientation

Determines the orientation of the page.

Portrait, Landscape

Portrait

whenNoDataType

Determines how to create reports...