This recipe teaches you how to use JasperReports to print a hardcopy of your Jasper report from within a Java Swing application.
Refer to the installPostgreSQL.txt
file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code for this chapter also includes a file named copySampleDataIntoPGS.txt
, which helps you create a database named jasperdb9
and copy sample data for this recipe into the database.
You will need Java Development Kit (JDK) version 1.6.X to compile and run the Java Swing application of this recipe. You will find all the necessary downloads and instructions on Sun's official website. After installing JDK, you will also set the JAVA_HOME
environment variable to point to the main folder of your JDK installation (..jdk1.6.X)
.
You will be using a JRXML file named MonthlyCustomerReport.jrxml
and three Java files named ConnectionManager.java, JasperReportsWrapper.java
, and JasperReportsPrinter.java
in this recipe. You will find these three files in the Task3
folder of the source code download for this chapter. In addition, you will also find two .bat
files named compile.bat
and run.bat
, along with two folders named com
and lib
in the same Task3
folder. Copy all five files and the com
and lib
folders (along with their contents) to the C:JasperReportsCookBookSamples
folder on your PC.
In order to compile JasperReportsPrinter.java
, you will need the following seven JAR files:
jasperreports-3.6.0.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-digester-1.7.jar
commons-logging-1.1.jar
groovy-all-1.5.5.jar
Your JasperReportsPrinter.java
will compile with these seven JAR files, but your complete application will not run without the JDBC JAR file that contains the driver for PostgreSQL. The name of the JAR file is postgresql-8.2-506.jdbc3.jar
. You can download PostgreSQL JDBC driver from http://jdbc.postgresql.org/download.html. You can also find this postgresql-8.2-506.jdbc3.jar
file in the ...JaspersoftiReport-nb-3.Xide8modulesext
folder of your iReport installation.
Once you have all the JAR files ready, you will put them in the C:JasperReportsCookBookSampleslib
folder on your PC. The compile.bat
and run.bat
files assume that all JAR files are located in C:JasperReportsCookBookSampleslib
folder.
Let's learn how to insert printing capability into a Java Swing application for JasperReports.
C:JasperReportsCookBookSamples
folder on your PC into which you copied files from the Task3
folder of the source code for this chapter. compile.bat
file. A command prompt will appear. It will compile JasperReportsPrinter.java
and show the compilation results. After successful compilation, dismiss the command prompt by clicking the Close button on its top-right corner. Now you will see that a JasperReportsPrinter.class
file will appear in the C:JasperReportsCookBookSamplescom
folder. run.bat
file to execute the compiled JasperReportsPrinter.class
file. A command prompt will appear, which will later show a GUI window containing a Print Hard Copy button. When you click the Print Hard Copy button, you will see Your report compile successfully and Your report filled with application data messages on the command prompt. Dismiss the command prompt by clicking the Close button. JasperReportsPrinter.java
file from the C:JasperReportsCookBookSamples
folder on your PC in a text editor. The JasperReportsPrinter.java
file contains the code for a class named JasperReportsPrinter
. You will find three methods named initComponents(), jButtonActionPerformed()
, and printHardCopy()
in the JasperReportsPrinter
class. The initComponents()
method initializes all the required components and the jButtonActionPerformed()
method handles the action when the Print Hard Copy button is clicked. You are about to write code for the third method named printHardCopy()
. This method will print a hard copy of your JasperReport. JasperReportsPrinter.java
by typing the following code in the printHardCopy()
method:// Print Jasper report if (jasperPrint != null) { try { JasperPrintManager.printReport(jasperPrint, true); } catch (JRException ex) { ex.printStackTrace(); } } else System.out.println ("Report printing failed");
After adding the code, the printReport()
method will become:
public void printHardCopy() { // Create wrapper instance and connect to DB JasperReportsWrapper wrapper = new JasperReportsWrapper(); Connection connection = wrapper.connect2DB( wrapper.dbServerAdd, wrapper.dbServerPort, wrapper.dbName, wrapper.dbUser, wrapper.dbPass); // Compile and fill Jasper report with application data JasperPrint jasperPrint = wrapper.fillReport( wrapper.compileJRXMLFile ( wrapper.path2JRXMLFile), null, connection); // Print Jasper report if (jasperPrint != null) { try { JasperPrintManager.printReport(jasperPrint, true); } catch (JRException ex) { ex.printStackTrace(); } } else System.out.println ("Report printing failed"); }
Save the JasperReportsPrinter
class.
compile.bat
file to compile the JasperReportsPrinter
class. A command prompt will appear. It will compile JasperReportsPrinter.java
and show the compilation results. Click the Close button to dismiss it. run.bat
file. It will execute your compiled Java application to show the same GUI window that your saw earlier in step 3. Click the Print Hard Copy button, and a Print dialog will appear. Click the OK button. Your JasperReport will be sent to printer for printing. Click the Close button on the top-right corner of the GUI as well as the command prompt to dismiss both.3.145.178.151