0% found this document useful (0 votes)
23 views

Pro Tutorial

This tutorial document provides guidance on using JReport Designer software. It contains 6 lessons that teach how to install and start JReport, create simple and advanced reports, incorporate charts, cross tabs and subreports, develop columnar reports, and use user data sources for reporting. The document outlines the content included in each lesson.

Uploaded by

rangga2300
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Pro Tutorial

This tutorial document provides guidance on using JReport Designer software. It contains 6 lessons that teach how to install and start JReport, create simple and advanced reports, incorporate charts, cross tabs and subreports, develop columnar reports, and use user data sources for reporting. The document outlines the content included in each lesson.

Uploaded by

rangga2300
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

JReport Designer − Tutorial

Table of Contents
Tutorial: Organization of This Part.....................................................................................................................................1

Lesson 1: Installing and Starting JReport.........................................................................................................................3


Introduction................................................................................................................................................................3
Installing JReport.......................................................................................................................................................3
Running JReport.......................................................................................................................................................3
Viewing the Demo Reports........................................................................................................................................3
Setting up JDBC Driver if Necessary........................................................................................................................4
Example 1: Installing a Sybase JDBC Driver...............................................................................................4
Example 2: Installing an Oracle JDBC Driver...............................................................................................5
Example 3: Installing an Interbase JDBC Driver..........................................................................................6

Lesson 2: Creating a Report From Report Wizard............................................................................................................7


Introduction................................................................................................................................................................7
Creating a Simple Report Using Report Wizard........................................................................................................8
Placing Parameters in Your Report.........................................................................................................................17

Lesson 3: Creating and Printing Reports From Multiple Tables...................................................................................21


Introduction..............................................................................................................................................................21
A Sample Standard Report with Multiple Tables.....................................................................................................21
Creating a Report With Multiple Tables...................................................................................................................22
Using Report Wizard..................................................................................................................................22
Formatting and Editing Your Rough Report...............................................................................................23
Adding Color to Your Report......................................................................................................................25
Adding Images to Your Report...................................................................................................................26
Adding and Editing Formulas and Summaries...........................................................................................26
Formatting Fields........................................................................................................................................30
Final Formats and Edits..............................................................................................................................30
Printing and Exporting to HTML.................................................................................................................32

Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports.....................................................................38


Introduction..............................................................................................................................................................38
Form Letters............................................................................................................................................................38
Charts and Graphs..................................................................................................................................................41
Cross Tab Reports..................................................................................................................................................46
Sub−reports.............................................................................................................................................................50

Lesson 5: Developing Columnar Report..........................................................................................................................54


Introduction..............................................................................................................................................................54
Developing a Columnar Report...............................................................................................................................54
Limits.......................................................................................................................................................................57

Lesson 6: Creating a Report with UDS.............................................................................................................................58


Introduction..............................................................................................................................................................58
Address Data Source Example...............................................................................................................................59
Compiling and Running Address UDS.......................................................................................................59
Adding Address UDS into JReport Catalog................................................................................................59
Making a Report with Address UDS...........................................................................................................61

I
Tutorial: Organization of This Part
The Tutorial book contains basic report building instructions and includes the reporting features that you will find useful.
The idea is that after you have walked through this Tutorial, you can easily follow the examples and build your own
reports. For in−depth feature coverage such as building reports on XML, EJB, stored procedure, SQL file, please refer to
the JReport Designer − User's Guide. This tutorial contains six lessons:

• Lesson One: Installing and starting JReport Designer, including basic data source and connection setup.
• Lesson Two: Creating a simple report with only a table. Also include the most actions for designing report.
• Lesson Three: Creating reports with multiple tables, learn simple report edit work using the designer. Get to know
JReport Query Editor and exporting feature.
• Lesson Four: Creating several typical reports − form letters, charts, cross tabs and Subreport.
• Lesson Five: Learning to develop a horizontal page break report.
• Lesson Six: Here is a very simple example showing how to use user data source to develop a report.

JReport Manual Roadmap

The following picture lists the other books in the JReport Designer manual, JReport Enterprise Server manual and
JREntViewer manual, and the relationship between them. You are advised to start with the JReport Designer manual.
With some report designing knowledge, it is easy to learn JReport Enterprise Server in the Client/Server architecture.

JReport Designer manual includes four books: Getting Started, Tutorial, User's Guide, User Reference.

JReport Enterprise Server (abbreviated as JREntServer) manual contains three books: Getting Started, Tutorial, User's
Guide. In addition, the JREntViewer manual is for using JREntViewer with JREntServer.

For a complete coverage of the features, operations and other information, please refer to the specific book:

JReport Designer Getting Started


An introduction to JReport. It covers issues associated with installation, setup and configuration of JReport. It is a
combination of informational topics and strategic references to various topics in JReport Designer − User's Guide and
JReport Designer − User Reference books.

JReport Designer User's Guide


In−depth coverage of JReport Designer's features and generally explains how to use the product. Browse to locate the

1
Tutorial: Organization of This Part

chapters that are of interest to you for step−by−step instructions.

JReport Designer User Reference


Specifications, providing technical, system and other reference information related to JReport built−in objects, built−in
formula functions and APIs.

JREntServer Getting Started


An introduction to JReport. It covers issues associated with installation, setup, and configuration of JReport Enterprise
Server. It describes most informational topics and references in the JREntServer − User's Guide.

JREntServer Tutorial
Basic Server configuration, administration and accessing instructions. After walking though the tutorial, you will be able to
use the server for managing the interaction between Client and Server that enables activities such as scheduling and
running reports.

JREntServer User's Guide


Procedures that will allow you to gain a more in−depth understanding of JREntServer. We have detailed the steps
involved in advanced Server features such as JSP, national language support, server API, application server integration,
JReport (Client) Viewer Bean.

JREntViewer Manual
Complete coverage of features and instruction for using JREntViewer with JREntServer. Client API usage is also
introduced in this book.

Index

2
Lesson 1: Installing and Starting JReport
Introduction
In this lesson, we assume that JReport Designer to C:\JReport of a Window 2000 system.

Installing JReport
To install JReport software, take the following steps.

1. Download the JReport install program jrpsetup.exe from the Jinfonet web site: http://www.jinfonet.com.
2. Run jrpsetp.exe and follow the prompts to install.

Running JReport
You can run JReport using either the shortcut or the batch file created by the setup program.

To run the batch file,

• From the desktop, double click My Computer, go to C:\JReport\bin\JReport.bat, run Jreport.bat.


• Or, from an MS−DOS command prompt, issue the following commands:

C:\>cd\jreport\bin
C:\jreport\bin\>jreport.

Viewing the Demo Reports


JReport Designer organizes report files and related objects into catalogs. You must open a catalog before you can view,
change, or create reports. A set of example reports are provided in the catalog called Tutorial.cat during the setup
process.

To open this catalog,

1. Start up JReport Designer using one of the methods above.


2. On the initial Open Report dialog, make sure that catalog in use is C:\JReport\Demo\Reports\Tutorial.cat.
3. If the catalog has not been properly set, press the Browse button to select the corresponding catalog from the
open dialog.

To view one of the example reports, click the Existing tab and select one of the reports in the list. You can choose one of
three browsers to inspect the design and various elements of a report.

• Resource Viewer
Organized into three tabs: Data, Reports and Styles. The Data tab diplays the query used by the current report
as well as DBFields, summaries, formulas and parameters available for the current report.
• Catalog Browser
Containing all resources that you can use to build a report. By default, the base query for this report is expanded
in the Catalog Browser. Data fields that are currently used in the report are shown with a red mark.
• Report Inspector
All the objects in the current report are organized into a tree structure on the top part of the Report Inspector
window, with the currently selected object highlighted. The properties and values of the current report objects are
shown on the bottom part. For most values, clicking the value will let you change it or select a new value from a
dialog.

3
Lesson 1: Installing and Starting JReport

You can now go to Lesson 2 to learn how to design a simple report using Report Wizard. However, if you are using a
database system that requires a native JDBC driver, you should follow the instructions below to correctly install the JDBC
driver.

Setting up JDBC Driver if Necessary


To show you how to connect to a database via a JDBC driver, we first provide a summary of the steps required, which is
followed by three examples. Here are the steps:

1. Install the JDBC driver according to the instructions of the JDBC driver provider.
2. For JReport Designer, edit %installroot%\bin\JReport.bat to append the classpath for the JDBC driver.
%installroot% is the path where you installed JReport Designer.
3. Connect to a database via the JDBC driver in JReport Designer by the following steps:

a). Start up JReport Designer. On the Open Report dialog, click Browse to open an existing catalog if no
catalog is selected. Click the Cancel button on the Open Report dialog.

b). Click Catalog Browser on the View menu to bring out Catalog browser. If you do not want to connect to
multiple database in one catalog, you can create a new catalog first by clicking New Catalog on the File menu.
c). Select the Default node in Catalog browser, and then click the New Datasource button on the toolbar.
Provide a name for the new data source (the first data source you connect to is set as Default by JReport
Designer, and cannot be modified or deleted) on the Input Database Name dialog to bring out the Get JDBC
Connection Information dialog.

d). Uncheck Use ODBC Data Source, which is checked by default.

e). Enter the URL of the database in the JDBC URL field on the Get JDBC Connection Information dialog. For
the format of the URL please refer to the instructions of your JDBC driver (An example has been provided in the
following: Installing a Sybase JDBC Driver section).

f). Enter your User Name and Password for the connection.

g). Check JDBC Driver, enter the JDBC driver class name in the JDBC Driver text box.

h). Click the OK button. The Add Table dialog box will appear to let you add tables into the catalog.

Example 1: Installing a Sybase JDBC Driver

The following example illustrates how to connect to a Sybase database server with the name SybServer, port No. 5000,
database pubs2, user ID sa, and password 123456:

1. Install Sybase JDBC driver Jconnect.class, run the SQL file sql_server.sql to create the JDBC metadata
tables. (Please refer to Sybase installation user's guide for further details.)
2. Edit %installroot% JReport\bin\JReport.bat to append the archive file of the driver to the classpath:

Change: ... −classpath "c:\JReport\lib\report.zip; ... lib\i18n.jar" ...


To : ... −classpath "c:\JReport\lib\report.zip;c:\jconnect\classes; ...
lib\i18n.jar" ...
3. Connect to the database via the JDBC driver in JReport Designer.

First, click Catalog Browser on the View menu to bring out Catalog browser.

4
Lesson 1: Installing and Starting JReport

4. Click the New Datasource button on the toolbar, provide a name for the new data source, and click OK to bring
out the Get JDBC Connection Information dialog.
5. Uncheck the check box Use ODBC Data Source and enter jdbc:sybase:Tds:SybServer:5000/pubs2 in the
JDBC URL field.
6. Enter the User ID sa and password 123456. Check the JDBC Driver check box. Enter the JDBC driver class
name com.sybase.jdbc.SybDriver in the JDBC Driver text box.

7. Another way to specify the JDBC driver name is to edit and add the driver name to the file
%InsallRoot%\bin\jdbcdrivers.properties.
8. Click the OK button. Then use the Add Table dialog to add tables into the catalog.

Example 2: Installing an Oracle JDBC Driver

The following example illustrates how to connect to an Oracle database server with the name p02_wuhb, port No. 1521,
database orcl, user ID scott, and password tiger:

1. Install Oracle JDBC driver classes12.zip/Classes12.jar (for Oracle 8i or 9i) or ojdbc14.jar (for Oracle 9i).
(Please refer to the user's guide of Oracle installation for further detail.)
2. Edit %installroot%JReport\bin\JReport.bat to append the archive file of Oracle thin driver to classpath:

Change: ... −classpath "c:\JReport\lib\report.zip; ... lib\i18n.jar" ...


To : ... −classpath "c:\JReport\lib\report.zip;c:\orc\thin\lib\classes111.zip; ...

5
Lesson 1: Installing and Starting JReport

lib\i18n.jar" ...
3. Connect to the database via the JDBC driver in JReport Designer.

First, click Catalog Browser on the View menu to bring out Catalog browser.
4. Click the New Datasource button on the toolbar, provide a name for the new data source, and click OK to bring
out the Get JDBC Connection Information dialog.
5. Uncheck the checkbox Use ODBC Data Source and enter jdbc:Oracle:thin:@p02_wuhb:1521:orcl in the
JDBC URL field.
6. Enter the User ID scott and password tiger. Check the JDBC Driver check box. Enter the JDBC driver class
name oracle.jdbc.driver.OracleDriver in the JDBC Driver text box.
7. Another way to specify the JDBC driver name is to edit and add the driver name to the file
%InstallRoot%\bin\jdbcdrivers.properties.
8. Click the OK button, then use the Add Table dialog to add tables into the catalog.

Example 3: Installing an Interbase JDBC Driver

The following example illustrates how to connect to an Interbase database server with the name InterbaseServer,
database demo.gdb, user ID TUTORIAL, and password TUTORIAL.

1. Install Interbase JDBC driver interclient.jar. (Please refer to the user's guide of Interbase client installation for
further detail.)
2. Edit %installroot%JReport\bin\JReport.bat to append the archive file of Interbase thin driver to classpath:

Change: ... −classpath "c:\JReport\lib\report.zip; ... lib\i18n.jar" ...


To : ... −classpath "c:\JReport\lib\report.zip;c:\interbase\lib\interclient.jar; ...
lib\i18n.jar" ...
3. Connect to the database via the JDBC driver in JReport Designer.

First, click Catalog Browser on the View menu to bring out Catalog browser.
4. Click the New Datasource button on the toolbar, provide a name for the new data source, and click OK to bring
out the Get JDBC Connection Information dialog.
5. Uncheck the check box Use ODBC Data Source and enter jdbc:interbase://InterbaseServer/c:\demo.gdb in
the JDBC URL field.
6. Enter the User ID TUTORIAL and password TUTORIAL. Check the JDBC Driver check box. Enter the JDBC
driver class name interbase.interclient.Driver in the JDBC Driver text box.
7. Another way to specify the JDBC driver name is to edit and add the driver name to the file
%InstallRoot%\bin\jdbcdrivers.properties.
8. Click the OK button, then use the Add Table dialog to add tables into the catalog.

Now that your data source is set up, you can go to Lesson 2 where you will learn how to design a simple report using
Report Wizard.

6
Lesson 2: Creating a Report From Report Wizard
Introduction
You can build a report with Report Wizard of JReport Designer, and you can modify an existing report by dragging and
dropping operations and changing the values of the object properties.

In this lesson, you will learn to create a simple report using Report Wizard.

JReport provides you with twelve kinds of report templates which can simplify your editing work. You can choose a
template to your liking in the initial Open Report dialog. Below is an example report with the title "Customer Sales
Report" in left alignment format. Detailed customer records are grouped by Country and within each country by Region.
The Sales from last year are totaled for each Region and for each Country.

Customer Sales Report

Country: USA
Region: CA
City Customer Name Phone Number Annual Sales

Cupertino The Island Coffee (408) 777−0001 $98,239.00

>Irvine Cafe International (714) 243−9548 $51,958.00

Los Gatos International Coffee Hut (650) 867−9879 $81,349.00

Palo Alto Automatic Java Machines (650) 239−4690 $74,986.00

San Francisco Cafe San Francisco (415) 288−9090 $67,823.00

Annual Sales Totals: $374,355.00

Region: NY

City Customer Name Phone Number Annual Sales

Dix Hill The Ultimate Bean (212) 938−4491 $54,738.00

New York Starmug Coffee (212) 834−9082 $101,001.00

New York Absolute Java (212) 946−3462 $50,600.00

Scarsdale The Java King (914) 657−9823 $78,321.00

Annual Sales Totals: $284,660.00

Annual Sales Total in the USA: $659,015.00

7
Lesson 2: Creating a Report From Report Wizard

Building a report from a database is time consuming, but with JReport Designer, it only takes a matter of minutes to
compile your data into a professional looking report.

Creating a Simple Report Using Report Wizard


1. Start JReport Designer according to the instructions from lesson one.
2. When the Open Report dialog appears, for this tutorial you should use the following catalog:

C:\jreport\Demo\Reports\Tutorial.cat

where c:\jreport is the path where you installed JReport Designer. You can use the Browse button to browse to
the catalog file.
3. With the New tab active, click the Left Alignment icon to select it.
4. Click the Create button to launch Report Wizard.
5. With the first four tabs of Report Wizard, you can specify the source of data and select data to be displayed in
the report.

Data − Selects the data source from all data objects to form a working set.
Joins − Joins multiple tables using JReport Designer's default settings or manually.
Conditions − Specifies the criteria which will retrieve data from the database to form the query of the report.
Display − Selects which fields to show in the report.

Let's first look at the Data tab. Here you should select a data source from the Catalog Data Source drop−down
list first, and then choose the objects you are interested in from the list below and place them into the Working
Set list. For this example, we select the Default data source, and in the list below, expand the Connection item.
Next, under the Tables node select the Customers table. Click the button to add all of the fields in the table
to the working set, or drag the fields to the Working Set directly. You can also expand the table and add
individual field one by one to the working set.

8
Lesson 2: Creating a Report From Report Wizard

By placing the objects in the working set, you have implicitly instructed JReport Designer to create a query, which
will retrieve all of the data you will need for the report.
6. Skip the Joins tab, since the report we are creating is a one−table report.
7. Click on the Conditions tab. You can specify search conditions in this tab.

You can type your search criteria both in the format of Filter and QBE.

In the Filter format, a search condition expression will appear on the screen, which enables you to type your
search criteria. The Expression includes not only the DBField selected, but also formulas and parameters. You
can also manually type the strings supported by the database.

If you select the QBE (Query By Example) format, you will specify the search criteria in a template resembling
the data record. All the data fields are shown to you, you just enter information that restricts the search to the
specified criteria.

Here we want to compare the sales in California and New York. Therefore, in QBE, type the following conditions.
Once finished, click the View SQL button to preview the SQL statement about your selection query so far.

9
Lesson 2: Creating a Report From Report Wizard

Notes:
♦ With QBE format, column names may not show completely. For example, column name
"Customers_Country" is cut off and shows only "Customers_Cou". To show the full column name, move
the mouse cursor to the line between two columns, till the cursor becomes a double−headed arrow, you
can move the line by dragging and dropping.
♦ You can adjust the order of the column. Move the mouse cursor on the column name, press the left
button, and drag the column to the place you want.
8. Click on the Display tab.

Usually not all of the fields from a query need to be visible in a report. You should indicate which fields to display
by selecting the objects from the working set and adding them to the list on the right.

For this example, select: City, Customer Name, Phone, Fax and Annual Sales to display in the report.

10
Lesson 2: Creating a Report From Report Wizard

Note: You can remove the selected field using button or change their order using buttons or on the
top of the list.
9. Click the Group tab.

Note: Records in a report are usually organized into groups based on certain criteria. With the Group/Sort tab
you can enter the criteria to define multi−level groups. You can also specify the ordering of groups and ordering
of records within each group.

We will group the data by selecting Country and then by Region. The arrows in the Direction column indicate
the sorting sequence of the groups. You can choose from descending order (c,b,a), ascending order (a,b,c) or
specify a special ordering criteria.

11
Lesson 2: Creating a Report From Report Wizard

10. Next you can sort records within the groups you created by clicking the Sort tab and selecting the sorting fields.
Here choose the City field to sort the records, as shown below.

12
Lesson 2: Creating a Report From Report Wizard

11. Click the Summary tab to specify summary functions.


To generate a total sales amount for each Region group, select Annual Sales, click to move it to the
Summary Fields. The Function drop−down list allows you to select a summary function such as sum, count,
minimum, maximum. We select Sum for this example to see a total of Annual Sales. The Sum Function is
computed when Break Field changes value at the end of a group. Clicking the Region field will cause a sum to
be created for each Region group.

To generate a total sum of annual sales in the country, move Annual Sales to Source Fields again, select the
Sum function for it. Leaving Break Fields empty. You can see Grandtotal in the Style column, JReport
Designer calculates the total annual sales.

13
Lesson 2: Creating a Report From Report Wizard

12. Click the Style tab to specify the page orientation.

14
Lesson 2: Creating a Report From Report Wizard

13. Click Finish to generate the report. The following is part of the report in Design mode.

When the report Design window appears, you can make any adjustment you like to the report layout.

15
Lesson 2: Creating a Report From Report Wizard

Report Inspector can be used to examine and change the objects in the report. Just highlight an object in
Report Inspector to change its properties, such as font, size, color.

For more information, please refer to User's Guide | Refining the Report Layout | Report Inspector.

Another simple way to edit your report is through dragging, dropping, and clicking the Toolbar and the Menu bar.
In Design mode, first highlight the object you want to edit by clicking it. When the cursor becomes a cross, you
can then move the highlighted object by dragging and dropping.

For this example, we have to manually move the column names from the PageHeader section to the second
GroupHeader section, and move report title to the PageHeader section.

For more information about how to edit the report layout, please refer to User's Guide | Refining the Report
Layout.

After editing, your report in Design mode may look like this:

14. Click on the View tab to run your report and view it. What you are seeing is still a rough copy of your report.

16
Lesson 2: Creating a Report From Report Wizard

15. Final work on your report.

Now you are ready to fine−tune your report and give it a professional look. Remember, Report Wizard provides
you with a quick start but JReport Designer provides a wealth of tools for you to adjust the report to your
preference. Use Report Inspector to change font, size, position, and other variables. Do not forget that you can
also move items anywhere in Design view. Try to insert labels and texts and place objects in headers/footers and
details.

Placing Parameters in Your Report


Frequently you need to specify a parameter when running a report. For example, you may want to input an order date as
an input parameter when running a report so that only orders on that date will be displayed. Typically, parameters are
used as part of the query conditions (search criteria) so that only records satisfying the specified parameter will be
retrieved to become part of the report.

1. Select DBField from the Insert menu list, and then on the Insert DBField dialog select the Parameters tab, or on
the Edit toolbar, click the Insert Parameter button directly.

17
Lesson 2: Creating a Report From Report Wizard

2. Click on the New button to create a new parameter.


3. The next window should appear after clicking on the new button. The example parameter we created below
allows the last person who edited the report to place his name on it.

18
Lesson 2: Creating a Report From Report Wizard

In the Parameter Name text box, type in a name for your parameter. We chose " Editor" for this example. In the
Prompting Text area, please enter a message that will appear. Our message was, "Please type in the name of
the last person who edited."

Note: You can only create a one line statement.


4. The Value Type should be left as String. This allows the parameter to remain the same as text. There are also
other Value Types.

Reference Notes: User Reference | JReport Objects | Parameter

Finally, place an example in the Default Prompt Value so that you have a format to follow. We chose "John
Smith" since our parameter deals with name.
5. Click OK to finish.
6. Select the new parameter and click the Insert button. Place it into your report.
7. Click on the View tab to view your report.
8. The following window will appear when you view the report.

You can type in any text field in the area. The default button will change the value back to its original value. In
our case, it was John Smith.
9. This was the final outcome of our report.

Design:

19
Lesson 2: Creating a Report From Report Wizard

View:

20
Lesson 3: Creating and Printing Reports From Multiple Tables
Introduction
In the last lesson, you learned how to create a report using a single table from the database. In this lesson you will learn
how to create a report that will take data from not one table, but several. Before going through the step−by−step
process of creating the report for this lesson, you may want to first examine the fully executed report called lesson3
which can be found in the Tutorial catalog.

In this lesson, you will learn:

• Creating a simple standard report with multiple tables


• Formatting and Editing Your Rough Report
• Adding Color to Your Report
• Adding Images to Your Report
• Adding and Editing Formulas and Summaries
• Formatting Fields
• Printing and Exporting to HTML

A Sample Standard Report with Multiple Tables


Jinfonet Gourmet Java
03/07/2001

Bill to: Washington Java Order ID# 3003


13220 Edward Lane Order Date 1/2/98
Washington D.C. 20183 Shipping VIA DRY Shipping Co.

Product Name Category Price Quantity (lbs) Discount Total


Chocolate
$9.00 50 $0.00 $450.00
Hazelnut Flavored
French Vanilla Flavored $9.00 20 $0.00 $180.00
French Roasted Roasted $5.95 50 $20.00 $277.50
Irish Cream Flavored $9.50 30 $0.00 $285.00
Straights &
Mocha Java $8.45 20 $0.00 $169.00
Blends
Vienna Roast Roasted $9.50 10 $0.00 $95.00/td>

Subtotal $1,456.00

Shipping $99.00

Tax $77.75

Total Order Amount $1632.75

This report can be created by JReport by simply adding fields from the Customers, Products, Orders, and Orders
Detail tables found in the demo database. Subtotals, Taxes and Total Order Amount are calculated using the Formula
and Summary objects of JReport.

21
Lesson 3: Creating and Printing Reports From Multiple Tables

Creating a Report With Multiple Tables


Using Report Wizard

1. Start JReport according to the instructions from lesson one and create a new report using the Standard template.
2. In the Data tab of Report Wizard, select Customers, Products, Orders, and Orders Detail tables as the
working set.
3. Click the Joins tab, and manually join fields by dragging and dropping them together. Join them as:

Orders Detail_Order ID = Orders_Order ID


Orders_Customer ID = Customers_Customer ID
Orders Detail_Product ID = Products_Product ID

Reference Note: For more information about Join, please refer to User’s Guide | Developing Reports with
Report Wizard | Join Tab

4. Click the Conditions tab. As introduced in Lesson 2 of this tutorial, there are two ways to specify the conditions:
Filter and QBE, here select QBE. Type >3010 in the Order ID column of the Orders table in order to see order
IDs greater than 3010.

22
Lesson 3: Creating and Printing Reports From Multiple Tables

5. Click the Display tab. Select:

Customers Table: Customer Name


Products Table: Product Name, Category
Orders Table: Shipping Cost, Order Date, Ship Via
Orders Detail Table: Unit Price, Quantity, Discount
6. Click the Group tab. For this example, we will group the data by Orders_Order ID, which means putting data
with the same Orders_Order ID together in groups. Choose Orders_Order ID to group by, in ascending order.
7. Skip Sort, Summary, Other and Style tabs. We are not focusing on these functions in this tutorial.
8. Now click Finish to generate the report.

Formatting and Editing Your Rough Report

1. So far, your report design looks something like this:

23
Lesson 3: Creating and Printing Reports From Multiple Tables

This is very rough and far from the nice report you would like to have. However, with JReport it is easy to format
this report and give it a professional look.

Start out by picturing in your mind where you want the data. Do you want to put the ordering information in the
group header and summary in the group footer? Perhaps drawing a rough sketch of your report on paper would
prove very helpful.
2. You can start by editing the system generated field labels into something more readable. Use the mouse to
select a label. You can edit the label directly by double clicking it. Resize, drag and drop labels to make them
look the way you want.
3. The example below can be used as a guide to organize fields. Use the View tab frequently to examine the
progress of your design.

24
Lesson 3: Creating and Printing Reports From Multiple Tables

Adding Color to Your Report

You can make your report look more interseting by adding color to it. JReport gives you the option of changing the
background and foreground color of any object.

1. Make sure all of your label objects are aligned as with the example shown above. Whenever you click an object,
it becomes highlighted and its property values are displayed in Report Inspector. You can manually adjust any of
its values. For example, you can change the width of a field precisely by entering the exact number of pixels.
2. Select the label field Product Name. Go to Report Inspector. Under Color select Background, and choose dark
blue. Then, under Color, select Foreground and choose white.

25
Lesson 3: Creating and Printing Reports From Multiple Tables

3. The result of your color selection will be instantly displayed on the screen in the below diagram:

Repeat step 2 for the rest your labels. Make your highlights look like the example below:

Note: A quicker way to change colors is to click and drag the mouse to define a box that selects all the objects at
once. Then use Report Inspector to change the color for all objects simultaneously.

Adding Images to Your Report

Adding an image to a report is a very simple task.

1. In the main design window, go to the Insert menu and select Image.
2. Select a GIF, BMP or JPEG file located in the catalog directory. For example here, we use the image file
Coffee.jpg under the catalog directory.
3. Place the image in the upper right corner (or anywhere else) on the report. You may resize it as necessary by
dragging the handles or by using Report Inspector.

Adding and Editing Formulas and Summaries

In this section, you will learn how to define formulas and summaries in Catalog Browser and add them to the report.

1. The example invoice report contains the following components:


♦ A billing address
♦ The Total price for each product ordered

26
Lesson 3: Creating and Printing Reports From Multiple Tables

♦ The total cost of all the products ordered


♦ The shipping charge and tax
♦ The Total cost of the invoice

Let's first add the billing address. A typical address contains the street, city, country and postal code. The
components are usually concatenated to form the full address. Care must be taken to skip the missing
components and trim off excess blanks. In JReport this can be done using Formula.

The Tutorial catalog provides two predefined formulas called Customeraddress12 and
city_state_postcode (see the table below) to format the address.

To access the formula editor from Catalog Browser, first select the View menu, and choose the Catalog Browser
item to bring out the Catalog Browser. Click the Data Tab, expand Formulas and right click on either
Customeraddress12 or city_state_postcode. Select Edit Formula to bring out the Formula Editor. The
examples are self−explanatory. A precise Syntax definition is available for your reference.

Customeraddress12 city_state_postcode
if ( IsNull( @"City" ) )
{
trim(@"Region") + ", " +
trim(@"Postal Code")
}
if ( IsNull( @"Address1" ) )
else if ( IsNull( @"Region" ) )
@"Address2"
{
else if ( IsNull( @"Address2" ) )
trim(@"City") + ", " +
@"Address1"
trim(@"Postal Code")
else
}
@"Address2" + ", " +
else
@"Address1"
{
trim(@"City") + ", " +
trim(@"Region") + ", " +
trim(@"Postal Code")
}

To add formulas to the report, on the toolbar of JReport Designer, click Insert Formula, select
Customeraddress12, and then click the Insert button and put it under the Customer Name section. Using the
same way to place city_state_postcode under the Customeraddress12 formula in the design window. Place
these formulas and delete the labels that come with them.

2. The next step is to figure out the total unit cost for each product. In Catalog Browser, double click Formulas, and
right click total. Select Edit Formula. You will then see the menu shown below. Enter the formula as below
where Unit Price, Quantity and Discount with a leading @ sign are the three field names of Orders Detail table.

Note: You can double click the field names to select them under Connection/hsqldb:.... The leading @ sign will
automatically added. If you manually type the sign to introduce the field name, make sure to add the table name
before the field name so as to distinguish the common field name. You are always recommended to press the
Check button before using a formula.

27
Lesson 3: Creating and Printing Reports From Multiple Tables

This formula calculates a total by multiplying the Unit Price by Quantity and subtracting the Discount. Click the
Check button. If everything is correct, click OK.

Next, click the Insert Formula button on the toolbar, select total and click the Insert button on the Insert DBField
dialog. Place this formula next to the Discount field as seen below. Because the total is computed once for each
Detail item, make sure that the field is located within the Detail section and the label is placed within the Group
Header section. Add colors as you did in the section titled "Adding Color to Your Report".

3. Summary functions are used to compute aggregated detail values. Select View |Catalog Browser to bring out
Catalog Browser and double click on Summary to reveal the existing summary functions. Right click on
Subtotal and then select Edit Summary. Notice how summary works, the function Sum takes the values from
the formula total over a group of detail records defined by identical Order_Orders ID. You can edit the summary
function by selecting from the Resource and pressing the button. Note that the break field you choose
must be the exact field you grouped your report at the very beginning of the Report Wizard.

28
Lesson 3: Creating and Printing Reports From Multiple Tables

4. Now close the Summary Editor dialog. In the Resource Viewer, select Subtotal, and then drag it to the Group
Footer as in the example.

5. The next formula we are adding is tax. Bring out Catalog Browser and double click on formula. Select Tax, right
click and select Edit Formula to see the formula: (@Subtotal+@"Shipping cost") * 0.05. Click OK. In the
Resource Viewer, select the Tax formula under the Formulas node, and then drag it to a position below the
shipping formula in the Group Footer section.

6. The last formula to be placed is the total of the subtotal, shipping cost and tax. In Catalog browser, double click
Formulas and select formula TotalAmount (if there isn't one, you need to create it). Right click and select Edit
Formula to see the formula @Subtotal + @"Shipping cost" + @Tax. Click the Check button to verify the formula.
Then click OK. Place the formula in the Group Footer section under the tax formula.

29
Lesson 3: Creating and Printing Reports From Multiple Tables

Formatting Fields

JReport permits you to define the result format for various field types including currency and date.

1. First, activate Report Inspector. Then, highlight the field Unit Price in the Detail Panel. Next, in Report Inspector,
find TextFormat and then click Format. In the drop−down menu, select $#,##0.00 which specifies a leading $
sign and the required comma and decimal places commonly used for currency.

2. Continue this formatting for all other currency data such as total, Subtotal, shipping cost, tax
and TotalAmount.
3. Similar format changes can be made for dates. Select any date field, and select the format MM−dd−yy in Report
Inspector.

Final Formats and Edits

There is still some editing and formatting to complete before this lesson is over. Begin by changing the names of the
labels so that they suit your need. Next, move data and titles to areas on the report where you feel appropriate.
Unnecessary headers and footers may be deleted by selecting the area and right clicking the mouse to reveal a menu.

30
Lesson 3: Creating and Printing Reports From Multiple Tables

Click on Hide to get rid of the unwanted section.

Usually each invoice page shows only one order. Since the report is grouped on Order_ID, we can simply make each
group occupy a separate page.

1. Click the background area of the Group Footer to select the section.
2. Bring out Report Inspector. Scroll down till you see a section called Others.
3. Under this section, find FillWholePage and select true in the drop−down list. This function will "fill" the entire
page with the section you highlight.

Our report looked like this in its final stage.

This is what it looks like if you view your report using the viewer.

31
Lesson 3: Creating and Printing Reports From Multiple Tables

Printing and Exporting to HTML

Once your report is finished, you may want to be able to print it or place it on the internet. JReport is ready to suit your
needs.

Printing Calibration

In order to overcome current limitation of JDK1.1.x, JReport needs to be calibrated on the very first print, so that lines
and text are printed correctly. Under File, select Print. A dialog should appear like the example below.

32
Lesson 3: Creating and Printing Reports From Multiple Tables

Please follow the instructions that are given to you on the screen. You only need to calibrate once for each printer that
uses JReport. After you complete this task, you may print the report by clicking the OK button.

Please note if you use JDK 1.2.x, or JDK 1.4.x, you will not come across this dialog when selecting Print.

Exporting to HTML

1. Placing JReport on the internet is fairly simple. Under File, go to Export To and select HTML, the following
dialog will appear.

33
Lesson 3: Creating and Printing Reports From Multiple Tables

2. On the Report tab, under Web Page Name, type in a name. Otherwise, JReport will use the report name as the
default HTML file name. Since JReport gives the extension *.html automatically, you don't need to type an
extension into the name of your report.

By default, JReport will convert the report into multiple files. That is, it designates a serial number for each HTML
page. For example, if you named your three−page report Sales, JReport will create three files called
Sales_1.html, Sales_2.html, and Sales_3.html, by pressing the radio button beside Single File.

The left Table Format check box designates whether you are using the <TABLE> tag format to convert the
report result or not. When you decide to export your report, you hope to convert it as what it looks in the Design
panel. The <DIV> tag, which describes the style (left and top coordinate) feature of the content to precisely
locate it in a page is newly released by HTML 4.0. However, the commonly used Internet Explorer or Netscape
can not always interpret <DIV> well. As a result, the designed position of an object in a page is often changed
when displayed in IE or Netscape. Therefore, JReport also supports the using of <TABLE> tag, which specifies
a table to organize data in a row−and−column table arrangement. Of course, this method can not precisely
locate the objects into a page, but it is supported by commonly used web browsers well and can be interpreted
correctly. By checking the Table Format box, you are specifying to use <TABLE> tag format.

Notes: Please be advised of the following when using the Table Format option to export to HTML:
When you choose to use the Table Format option to lay out the objects of the report in HTML, if two objects
overlap JReport will throw one away. In addition, any geometric object (Lines, boxes, arc...) will be thrown away
too. Why? If we define a cell unit (size) as one pixel in width and one pixel in height, the resulting file will be
extremely large, so we use the default size of the grid in JReport Designer to define the cell size of the table.
When you design your report, you can choose the "Snap To Grid" option to get a very satisfactory result in
HTML. Remember that it is better to not change the default grid size in JReport Designer. If the default grid size
is changed however, we cannot guarantee a satisfactory result.

34
Lesson 3: Creating and Printing Reports From Multiple Tables

3. On the Directory tab, you can specify the directory where you want the HTML files to be saved. Otherwise,
JReport will use a default directory.

4. By clicking the ChartApplet tab, you have the option of displaying the charts you have placed in your report in
HTML format. By default, JReport will not show charts in HTML, so if you want such images to be seen,
select Embed chart applet.

35
Lesson 3: Creating and Printing Reports From Multiple Tables

5. It is recommended that you read the contents on the Note Tab before you convert your report to HTML format.

36
Lesson 3: Creating and Printing Reports From Multiple Tables

6. After all steps have been taken, click the Finish button to convert your report to HTML.
7. You can now test your exported HTML files by opening them in a web browser.

37
Lesson 4: Creating Form Letters, Charts, Cross Tabs and
Sub−reports
Introduction
JReport not only creates a standard report, but can also build dynamic and exciting formats for you. In this lesson, we will
explore some of these other report formats. We will focus on four types:

• Form Letters
• Charts and Graphs
• Cross Tab Reports
• Sub−reports

Form Letters
Form letters are used so that you can create pre−defined standard letters with little difficulty. Let's take a look at an
example of a form letter.

___________________________________________________________

Jinfonet Gourmet Java August 18, 2000


4099 Hathaway Road
Rockville, MD 20906

Absolute Java
34826 Atwood Drive
New York City, NY 1004

Dear Mr. Smith,

The current statement displays your account at our company.


Your bill comes out to be $550.00. Thank you for making your coffee
purchases with Jinfonet Gourmet Java.

Sincerely,

Jinfonet Gourmet Java

___________________________________________________________

This is a letter to just one customer. What if you want to send the same letter to a thousand different customers with their
individual account balances? By using the form letter, you can use fields to place the data from your database onto your
letters. The following shows you the steps for creating a form letter.

1. Assuming that you have connected your database as per lesson one, start JReport and select the Standard icon
on the New tab of the Open Report dialog, then click the Create button. The Report Form Creation Wizard will
then appear with the Data tab selected.

Assuming you have completed lesson two, please perform the following steps for each tab to create a form letter.

Data: Select the Customers table located under Connection/hsqldb:.../Tables and place it in the working set.

Conditions: Select QBE, then under the Country column, type USA, and under the Region, type CA (1st row) and

38
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

NY (2nd row). In this example, we are sending statements to customers in these two states.

Note: When inputting conditions in the QBE format, for example, you want to filter data in USA, you can just type
USA instead of ='USA' under the Country column, JReport will automatically format USA to ='USA'.

Display: Under Tables, select Customer Name. Under Formulas, select customeraddress12 and
city_state_postcode. Note that we are using formulas here to combine the address components from several
fields into a two−line address.

Group: Add the Customers_Country and Customers_Region fields from the working set.

Sort: Add the Customers_City field.


2. Now, you can click the Finish button to generate the report. We will insert the letter body in the design mode by
inserting a text object.

3. In the design mode, click the Insert Text button or under the Insert menu select the Text item. Insert the text
box in the Detail section. Then, double click the text box, in the blank area type in the following:

The current statement displays your account at our company. Your bill comes out to be

After the word "be", insert DBField by clicking the Insert DBField or the on the Insert menu, click DBField, and
select the Annual Sales field from the Customer table and click on the Insert button. Then, adjust the size of the
DBField so that it fits its grid. Continue the rest of the dialog in the Form Letter menu with this statement:

Thank you for making your coffee purchases with Jinfonet Gourmet Java.

Your overall statement in the menu should look like this:

The current statement displays your account at our company. Your bill comes out to be $#,##0.00. Thank
you for making your coffee purchases with Jinfonet Gourmet Java.

Note that the Annual Sales field you inserted is represented by a format $#,##0.00.
4. In lesson three, you dealt with changing formats. Highlight the field you inserted in the Form Letter menu. This
will bring up the properties of this particular field just like Report Inspector does. You can change the properties
of the field in this area, for example, you can delete an unwanted field and adjust the size and position of
individual object. Text can also be highlighted as well to bring up its properties.
5. This is what your rough letter will look like:

39
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

Now you are ready to give it the finishing touch. If you have completed Lessons 2 and 3, you should be
accustomed to moving fields, changing fonts, and inserting objects. However there is still one step that needs to
be mentioned. If you would like to change the format of your field from #,##0,### to another format such as
$#,##0.00, double click on the text box. Then highlight the field and view Report Inspector. Under TextFormat, go
to Format. From the drop−down list, select the appropriate format.
6. When our letter was completed it should look like this:

Design Mode

View Mode

40
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

If you would like another example of a form letter, select and explore the report TextObjectEmbedded.cls in
the Tutorial.cat catalog file. You can also look at FormLetter.cls for more details on the form letter we
created.

Charts and Graphs


Charts and graphs create graphical views of summarized data. They can help you visualize the data in a report. This
section will show you how to create and place charts and graphs into your reports.

1. Select the Left Alignment icon on the New tab of the Open Report dialog.
2. Assuming that you have completed Lesson 2, place the following in each tab so that we build the basic structure
for our report.

Data: Select the Customer table and place it in the working set.

Conditions: Select QBE, under the Country column, type "USA", and under Region, type "CA" (1st row), "NY"
(2nd row), "GA" (3rd row), "MD" (4th row) and "TX" (5th row).

Display: Under Tables, select Customer Name and Annual Sales.

Group: Add the Customers_Country and Customers_Region fields from the working set.

Summary: Under Field, select Annual Sales. Under Function select Sum, and under Breakfield select
Customers_Region. Since we chose Annual Sales for the summary, our graph will now display graphical
analysis of this data.

So far, you can finish building your report by clicking the Finish button and insert the chart in the Design mode.
3. In the Design mode, select Chart on the Insert menu or click the Insert Chart button on the Edit toolbar. The
following dialog will then appear:

41
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

Select any of the graph formats. Refer to the User Reference book for more information on the types of graphs.
For this tutorial we chose 3D Bars (side by side).
4. Then, click the Report Data tab. In the Graph Position panel, choose a specific location (Detail, Group
Header/Footer, etc) for the graph. We want to view the results within the country, USA, so we
selected Customer_Country from the drop−down list, and check the Footer radio button to place our graph in
the Group Footer.

Select the summary Sum_Annual Sales2 under Customers_Region and add it to the Show Data list. JReport
Designer will add the DBField Customers_Region to the Category Names list automatically.

42
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

5. Click the OK button and make some necessary adjustments. When you are done, your report might be like this:

43
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

6. Once again you can do some final editing here. A new tip to learn here is that if you first double click the graph,
then click on the graph and drag the mouse, you can rotate the graph. This is only available for 3D graphs such
as the 3D Bars and the Pie Chart.

For example, you can add a title to the chart, to do this,


1. Select the chart platform and right−click the mouse.
2. Click the Add Label item on the pop−up menu and a label will be displayed on the top left corner.
3. Selected the label and in the Report Inspector change the following properties:

Text: Graphical Analysis of Annual Sales


FontFace: Vernada
FontStyle: Bold
FontColor: 0x0000c0

You can also change the properties using another way: select the label and right−click, click Format
Label to bring out the Chart Label dialog and then set the corresponding options.

Note: The graph you see in the Design mode is not exactly the same as the graph you see in the View mode. Do
not worry if there are values that do not make sense in the Design area. They are only there so that you can get
a sense of what your chart may look like.

Below might be the view result of your final graph:

44
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

View (First Page of the report)

Last Page

45
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

If you would like another example of a chart and graph report, select and explore the report salestrend.cls and
regionsalesgraph.cls in the Tutorial.cat catalog file. You may also look at Lesson4b.cls for more details on the
chart and graph report we created.

Cross Tab Reports


A Cross Tab is an object that summarizes data and presents the summaries in a compact row and column format. This
allows comparisons and identification of trends to be displayed. Below is an example of a Cross Tab object.

In the following section, you will learn to create a Cross Tab report.

1. Assuming that you have connected your database as per Lesson 1, start JReport and select the Cross−Tab icon
under the New tab in the Open Report dialog. Click on the Create button.
2. The Report Form Creation Wizard will then appear.

Assuming that you have completed Lesson 2, perform the following steps for each tab to create a cross−tab
report.

Data: Select the Customers, Products, Orders Detail and Orders tables under the Tables node and place it in
the working set.

Joins: Join the tables manually like this: Orders_Order ID = Orders Detail_Order ID, Orders_Customer ID =
Customers_Customer ID, Orders Detail_Product ID = Products_Product ID.

Conditions: Select QBE. Under the Country column, type USA, and under the Region, type CA.
3. Click on the Cross Tab tab. The following dialog should appear:

46
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

The Field menu lists the working set you created. You can select any of the fields or formulas given to you in this
menu. Select Customer Name under the Customers table, then click to place your field(s) into the row
section of the Cross Tab. Then, select Category under the Products table, and click to place your field into
the column section.
4. Select Price under the Products table and click to place it into the Aggregations menu. Then, highlight the
Price field under the Aggregations menu, and click the Average function beside Price. This drop down list lists
all the functions you can use for your fields in the Aggregation menu. Select the Sum function for the Price field.

47
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

Next, repeat this step to select the Annual Sales field under the Customer table and implement the
Sum function for this field.
5. Highlight the Customer Name field under the Rows menu, and select the palette box beside the field. The
following window will pop up.

48
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

You can press Customer to open the palette for more colors. In this example we simply select the dark blue
color.

Repeat this step for the Category field under the Columns menu.
6. After all this is done, click the Finish button. Your report should look like this:

7. Start editing and fine tuning the Cross Tab. We suggest you create a legend for your summarized fields since
JReport does not place a label by these fields. This is what our report looked like after editing.

Design

49
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

View

Sub−reports
JReport allows a report (referred to as sub−report) to be inserted into another report (referred to as the primary report).
There is no difference between the primary report and the sub−report. A sub−report can be standalone, i.e. it can be run
separately like a normal report or it can also include another sub−report.

You can create sub−report object in two ways:

• Use Sub−report report wizard.


• Click Insert | Subreport menu item or the Insert Subreport button on the Edit toolbar.

In this lesson you will learn how to create a report with sub−report object by report wizard. We will build a report that
prints all employees whose salaries are lower than the group average salary in groups step by step. You can create it by
following these steps.

50
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

1. Create a report which will be used as a sub−report.

Assuming you have completed Lesson 2, perform the following steps for each tab to create a standard report as
the sub−report that you want to insert. We will create a report file named report SalaryList.cls.

Data: Select the Employee table under Connection/hsqldb:.../Tables and add it into the working set.

Display: Add Employee Name, Employee_Employee ID, Last Name, First Name, Position, Hire Date,
Salary fields into the report.
2. Create a report with sub−report by report wizard. Then, select the Subreport icon under the New tab in the Open
Report dialog.
3. Click the Create button , and the Report Form Creation Wizard will then appear.

Assuming you have completed Lesson 2, please perform the following steps for each tab to create the primary
report.

Data: Select the Employee table under Connection/hsqldb:.../Tables and add it into the working set, set query
name is Lesson4d.

Group: Under the Group tab, add the Reports To field from the working set.

Summary: Under the Summary tab, add the aggregate fields: Average function on Salary, break by Reports To.
4. Click on the Subreport tab. you will see the following panel:

51
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

Style: Display the type of the report.


Section: Display the section which the sub−report will appear.
Choose a report: Click Browse button to select the report SalaryList.cls as sub−report.
Links: The relationship between the primary report and the sub−report. When you build a link, JReport will match
up the records with equal relationships within the sub−report and the primary report.

Here we create two links: "Reports to = Reports to", "Average_Salary2 >= Salary". Select the field "Report to" in
the Field in main report box, click to add the field into the Links box. In the Links box, choose the
operator " = " from the OP drop down list. Then select the field "Report to" from the Fields (Subreport) drop
down list. Create link "Average_Salary2 >= Salary" using the same method.
5. After finishing the report, you will have the following report in the design mode:

6. Edit the report:

Select the Detail section, then go to Report Inspector, set property Invisible to true.

Place the subreport to the GroupHeader section.

Delete the column name "Salary" in PageHeader.

Select field Salary, change format #,##0.### to $#,##0.0# in Report Inspector. Also change the format of
summary field Average_Salary2 to $#,##0.0#.

Select subreport "SalaryList.cls", set background to light gray in Report Inspector.

Now the report in the design mode will look something like this:

52
Lesson 4: Creating Form Letters, Charts, Cross Tabs and Sub−reports

7. View the report. You will now see the report result as shown below:

53
Lesson 5: Developing Columnar Report
Introduction
Columnar report is used to create a horizontally aligned report. Let's take a look at an example:

Employee Information
Employee ID 1 2 3 4
First Name Laurena Jonathan Jerenny Jennifer
Last Name Croft Hopkins Miner Lee
Position Sale Representative Sale Representative Marketing Vice President
Hire Date 09/12/1984 04/03/1987 05/19/1979 06/09/1981
Home Phone ( 301) 823−7424 ( 301) 946−2349 (410) 349−2834 (703) 223−4422

Developing a Columnar Report


To do such a report, follow the steps below:

1. Assuming that you have connected your database as in Lesson 1, start JReport and select HBlank icon under
the New tab on the Open Report dialog. Click the Create button.
2. The following window will appear.

54
Lesson 5: Developing Columnar Report

Assuming you have completed Lesson 2, perform the following steps for each tab to create a columnar report.

Data: Select the Employee table under the Tables node and place it in the working set.

Conditions: Select QBE, then under the Employee_Employee ID column, type < 5.
3. Now, you can click the Finish button to generate the report. You will find that all the sections are aligned
horizontally as below:

4. Select Label from the Insert menu, and place the label into the ReportHeader section. Double click the label to
edit it. Type "Employee Information". Then modify the font of the label and resize the label to fully display the text
of label.
5. Click the Insert DBField button on the Edit toolbar to access the Insert DBfield dialog box, and select the fields
Employ_Employee ID, First Name, Last Name, Position, Hire Date, Home Phone (use Ctrl key to select
multiple fields), check the Vertical radio button, then click the Insert button to insert them into the Detail section.

55
Lesson 5: Developing Columnar Report

6. After refining the layout, your report will look something like this:

7. Select the Report Header section and open Report Inspector, set the property Underlay to be true. This will align
the title Employee Information with the data.
8. Now, you can view your report as below.

56
Lesson 5: Developing Columnar Report

Limits
Currently, the horizontal report has the following limitations:

• Fields and objects cannot cross pages. That is, you cannot set the section property CanCrossPage = true. Also,
you cannot insert an object, such as a Cross−tab object, which will cross pages into your report. In these cases,
the result will sometimes encounter perblems (not completed).
• In order to support a mailing label report, there is a property named TileDetailSection for detail section. However,
it does not work in this type of report, So you cannot set this property to be true.
• Some properties of fields, such as AutoFit, WordWrap, do not function.
• You can insert other normal reports into this type of report if they do not cross pages. Otherwise, the sub−report
contents will be cut off.

57
Lesson 6: Creating a Report with UDS
Introduction
As a database application, JReport Designer can access data stored in a database through the JDBC connection. Now,
through User Data Source API, JReport Designer can also access data from an external data source, such as a text file,
Lotus Notes etc, which is not stored in a database, or when no JDBC driver is available.

The UDS API is a part of JReport Data Access Model as shown in this diagram:

The User Data Source API

• is a Java interface that provides a data set to JReport.


• has two methods you need to implement. Below is the Java code of class definition:

public class SQLDataSource implements JRUserDataSource


{
// Define data.
public ResultSet getResultSet(String param) throws JRUserDataSourceException
{
// Method body.
}

/**
* Free the resources such as : Connection, Statement, ResultSet.
*/
public void releaseResultSet() throws JRUserDataSourceException
{
// Method body.
}
}

58
Lesson 6: Creating a Report with UDS

• The parameter string param can be changed when the UDS object is added into JReport Designer. The result
set may be different according to the PARAMETER string. It is very flexible and convenient for users.

Address Data Source Example


This example is a plain text file AddressesList.txt which contains the data information like a dummy table:

Laurena Croft 34826 Atwood St. New York City NY 10004 USA...
Jonathan Hopkins 5062 Brandon Green Ave. Minneapolis MN 55402 USA...
Jeremy Miner 9283 Cherry Leaf Lane Palo Alto CA 94303 USA...
....

You can see each line sequentially records the name, address, state, region, zip code, country, email address and other
information. We will show you how to build a report using the data stored in a formatted text file. You can modify this
example to satisfy your requirements.

Compiling and Running Address UDS

The data file is AddressesList.txt. There are three classes used in this example and their source codes are:
AddressListUDS.java, AddressListResultSet.java and AddressListResultSetMetaData.java. These files are available on
our web site at http://www.jinfonet.com/webpage/kbase/kbase.htm (manual related materials).

If you use your own UDS classes, you must be sure about one thing: the class files can be found while running.
Therefore, the directory should be classpath/package name. Suppose you have installed the help of JReport Designer,
the help folder will be under %JReportPro_Installroot%. For these demo UDS classes, classes belong to a package
named "help". You may copy the above necessary files under %JReportPro_Installroot%\help, and add the additional
entry into classpath statement of the batch file starting JReport: %installroot%.

For example, on Windows, if you install JReport into the default directory "c:\JReport" and all the UDS classes are
located under c:\jreport\help, you need the following entry in the batch file c:\JReport\bin\JReport.bat:
...java −classpath

Compile the example: go to the directory %installroot%\help and enter:


c:\jreport;c:\jreport\lib\report.zip;c:\jreport\lib\JREngine.zip

On Unix, if you install JReport into the /usr/jreport:


classpath = %classpath%:/usr/jreport:

javac −classpath ".;%Installroot%\lib\JREngine.zip;" Address*.java

Adding Address UDS into JReport Catalog

1. After compilation, start JReport with the batch file you just modified. You are prompted with the demo catalog
and reports. Then, click Cancel.
2. Since UDS API connects with user data source independent of the JDBC connection, you can then create
another catalog instead of building reports in the demo catalog Tutorial.cat. To do this, go to File | New Catalog.
Here, we create a new catalog UDS.cat.
3. In Catalog Browser, right click UserDataSource | General...

59
Lesson 6: Creating a Report with UDS

4. In the Add User Data Source dialog, enter the following information, and then click OK:

5. Now expand the added Address UDS and you will see the table−like tree structure.

60
Lesson 6: Creating a Report with UDS

Making a Report with Address UDS

Now go to File | New menu item or click the New Report button on the standard Toolbar to launch Report Wizard (A
step by step report designer assistant that help you build your reports). Now let us view the result of our demo
lesson7b.cls.

61
Lesson 6: Creating a Report with UDS

Edit your report afterwards to give it a professional appearance.

62

You might also like