How to use Jasper Reports in JavaFX 8 – Sample Example Tutorial

By | December 20, 2016

Jasper Reports are used for showing information that need to be printed or saved as document file.

Use Cases:

– For printing Reciepts
– Bill/ Invoice
– Detailed Reports
– and so on..

If you have already developed some Java application/ Software using JavaFx and now need to print some data from the software, then this is the tutorial for you.

Jasper Reports are designed in the form of .jrxml files similar to .fxml files of javaFX.
We need to access these .jrxml files in java class and pass the data to be filled in the Jasper report using Java Objects(List) or through an SQL query.

Steps required to achieve this:

1) Install Jaspersoft Studio for creating .jrxml file using drag and drop.
2) Get Jasper Reports Library .jar files to be added in libs folder of your JavaFX project.
4) Design .jrxml file using JasperSoft Studio.
3) Write Java code to show .jrxml file as Report.

Install Jaspersoft Studio

You can either download Jaspersoft studio plugin for eclipse OR download Standalone Jasper soft Studio. We just need the .jrxml report files designed using either of the above.

1) Eclipse Plugin: Search JasperSoft in Eclipse MarketPlace and install the latest release.
jaspersoft studio javafx download plugin

2) Standalone JasperSoft Studio: Download


Get Jasper Reports Library

We need jasper library for java project. To get the library files. Download the Jasper Reports Library from here.

jasper Reports Library for Java download

Download the “jasperreports-6.3.1-project.zip” in the above screenshot.

After extracting the zip file you will get the project folder in which “dist” folder is of our concern.
jasper dist folder
There are 3 .jar files in this dist folder. Add all the three files to the libs folder of your javaFX project.
But these 3 jar files are not enough. There are many other dependencies require by Jasper Reports which should be again added to the libs folder of your project.

The Jasper Reports Project uses Apache Ant, Apache Ivy – as the dependency Manager. We need to get the Apache Ant/Ivy for getting the remaing .jar files.

We have to Download Apache Ant and run the commands on Jasper Reports Project’s root directory using Terminal/Command Prompt.

Download Apache Ivy, Apache Ant from here:
Ant
(You can alway get the latest releases by searching on Google)

Extract the downloaded .zip file and copy the folder ant anywhere near the Jasper Reports Project folder.
Directory structure of ant and jasper project

Now run the following command in the root directory of the Jasper Reports project:

java -jar ../ant/ant-launcher.jar retrievelibs

Look at the screenshot showing how I ran the above command:

terminal showing ant retrieving Jasper reports library for java

The required dependency .jar files will be downloaded into the dist/libs folder of the JasperReports project.
Copy all the .jar files from this lib folder to the libs folder of your java project.

jasper dependency jar files

NOTE: Add all the jar files to the Build Path of your Java project.
Here is the list of all the jar files that will be added to the libs folder of your JavaFX Project:

  1. ant-1.7.1.jar
  2. antlr-2.7.5.jar
  3. asm-all-4.0.jar
  4. barbecue-1.5-beta1.jar
  5. barcode4j-2.1.jar
  6. batik-anim-1.8.jar
  7. batik-awt-util-1.8.jar
  8. batik-bridge-1.8.jar
  9. batik-css-1.8.jar
  10. batik-dom-1.8.jar
  11. batik-gvt-1.8.jar
  12. batik-svg-dom-1.8.jar
  13. batik-svggen-1.8.jar
  14. batik-util-1.8.jar
  15. bsh-2.0b4.jar
  16. castor-core-1.3.3.jar
  17. castor-xml-1.3.3.jar
  18. commons-beanutils-1.9.0.jar
  19. commons-collections-3.2.2.jar
  20. commons-digester-2.1.jar
  21. commons-javaflow-20160505.jar
  22. commons-lang-2.6.jar
  23. commons-logging-1.1.1.jar
  24. core-3.2.1.jar
  25. ecj-4.3.1.jar
  26. groovy-all-2.4.5.jar
  27. hibernate-core-3.3.2.GA.jar
  28. httpclient-4.3.4.jar
  29. httpcore-4.3.2.jar
  30. icu4j-57.1.jar
  31. itext-2.1.7.js5.jar
  32. jackson-annotations-2.1.4.jar
  33. jackson-core-2.1.4.jar
  34. jackson-databind-2.1.4.jar
  35. jasperreports-6.3.1.jar
  36. jasperreports-fonts-6.3.1.jar
  37. jasperreports-javaflow-6.3.1.jar
  38. jaxen-1.1.1.jar
  39. jcommon-1.0.23.jar
  40. jfreechart-1.0.19.jar
  41. jxl-2.6.10.jar
  42. lucene-analyzers-common-4.5.1.jar
  43. lucene-core-4.5.1.jar
  44. lucene-queryparser-4.5.1.jar
  45. mondrian-3.1.1.12687.jar
  46. olap4j-0.9.7.309-JS-3.jar
  47. persistence-api-1.0.jar
  48. poi-3.10.1.jar
  49. poi-ooxml-3.10.1.jar
  50. rhino-1.7.6.jar
  51. servlet-api-2.4.jar
  52. spring-beans-2.5.jar
  53. spring-core-2.5.jar
  54. velocity-1.7.jar
  55. xalan-2.7.2.jar
  56. xml-apis-ext-1.3.04.jar
  57. xmpcore-5.1.1.jar

Design .jrxml file using JasperSoft Studio.

Open Jasper Studio and create a new Jasper Report as shown below:

Creating new Jasper Report

Now select Blank A4 as the Report Template as shown below:

Jasper Report Template

Choose the name and directory for the jrxml file and at the screen asking for Data Source, select “New Data Adapter – Empty Rows” from the drop down as shown below:

Selecting data source for jasper report

Click Next->Finish to create the jrxml file. Look at the screenshot below:

Jasper new reports

You can download the sample .jrxml file here: Blank_A4.jrxml
Create a folder named data in your java project and Copy the created .jrxml file there.
Now create a java file named PrintReport.java as follows:

package com.marothiatechs.reports;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.swing.JRViewer;

public class PrintReport extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void showReport() throws JRException, ClassNotFoundException, SQLException {

		String reportSrcFile = "data/Blank_A4.jrxml";

		// First, compile jrxml file.
		JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
		// Fields for report
		HashMap<String, Object> parameters = new HashMap<String, Object>();

		parameters.put("company", "MAROTHIA TECHS");
		parameters.put("receipt_no", "RE101".toString());
		parameters.put("name", "Khushboo");
		parameters.put("amount", "10000");
		parameters.put("receipt_for", "EMI Payment");
		parameters.put("date", "20-12-2016");
		parameters.put("contact", "98763178".toString());

		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		list.add(parameters);

		JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(list);
		JasperPrint print = JasperFillManager.fillReport(jasperReport, null, beanColDataSource);
		JRViewer viewer = new JRViewer(print);
		viewer.setOpaque(true);
		viewer.setVisible(true);
		this.add(viewer);
		this.setSize(700, 500);
		this.setVisible(true);
		System.out.print("Done!");

	}

}

Now call the source code for the Main.Java is as follows:

package com.marothiatechs.finance;

import java.sql.SQLException;

import com.marothiatechs.reports.PrintReport;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import net.sf.jasperreports.engine.JRException;

public class Main extends Application {

	Stage window;
	Scene scene;
	Button button;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		window = primaryStage;
		window.setTitle("Jasper Report Tutorial");
		button = new Button("Show Receipt");
		button.setOnAction(e -> {
			try {
				// --- Show Jasper Report on click-----
				new PrintReport().showReport();
			} catch (ClassNotFoundException | JRException | SQLException e1) {
				e1.printStackTrace();
			}
		});

		VBox layout = new VBox(10);
		layout.setPadding(new Insets(20, 20, 20, 20));
		layout.getChildren().addAll(button);
		scene = new Scene(layout, 300, 250);
		window.setScene(scene);
		window.show();
	}

}

Now Run the Main.java file as Java Application:
Jasper Report running in JavaFX JFrame

1,309 total views, 1 views today

(Visited 973 times, 1 visits today)