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.
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.
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.
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.
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:
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.
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:
- ant-1.7.1.jar
- antlr-2.7.5.jar
- asm-all-4.0.jar
- barbecue-1.5-beta1.jar
- barcode4j-2.1.jar
- batik-anim-1.8.jar
- batik-awt-util-1.8.jar
- batik-bridge-1.8.jar
- batik-css-1.8.jar
- batik-dom-1.8.jar
- batik-gvt-1.8.jar
- batik-svg-dom-1.8.jar
- batik-svggen-1.8.jar
- batik-util-1.8.jar
- bsh-2.0b4.jar
- castor-core-1.3.3.jar
- castor-xml-1.3.3.jar
- commons-beanutils-1.9.0.jar
- commons-collections-3.2.2.jar
- commons-digester-2.1.jar
- commons-javaflow-20160505.jar
- commons-lang-2.6.jar
- commons-logging-1.1.1.jar
- core-3.2.1.jar
- ecj-4.3.1.jar
- groovy-all-2.4.5.jar
- hibernate-core-3.3.2.GA.jar
- httpclient-4.3.4.jar
- httpcore-4.3.2.jar
- icu4j-57.1.jar
- itext-2.1.7.js5.jar
- jackson-annotations-2.1.4.jar
- jackson-core-2.1.4.jar
- jackson-databind-2.1.4.jar
- jasperreports-6.3.1.jar
- jasperreports-fonts-6.3.1.jar
- jasperreports-javaflow-6.3.1.jar
- jaxen-1.1.1.jar
- jcommon-1.0.23.jar
- jfreechart-1.0.19.jar
- jxl-2.6.10.jar
- lucene-analyzers-common-4.5.1.jar
- lucene-core-4.5.1.jar
- lucene-queryparser-4.5.1.jar
- mondrian-3.1.1.12687.jar
- olap4j-0.9.7.309-JS-3.jar
- persistence-api-1.0.jar
- poi-3.10.1.jar
- poi-ooxml-3.10.1.jar
- rhino-1.7.6.jar
- servlet-api-2.4.jar
- spring-beans-2.5.jar
- spring-core-2.5.jar
- velocity-1.7.jar
- xalan-2.7.2.jar
- xml-apis-ext-1.3.04.jar
- xmpcore-5.1.1.jar
Design .jrxml file using JasperSoft Studio.
Open Jasper Studio and create a new Jasper Report as shown below:
Now select Blank A4 as the Report Template as shown below:
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:
Click Next->Finish to create the jrxml file. Look at the screenshot below:
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:
30,851 total views, 20 views today