Hot questions for Using JasperReports in javafx

Question:

I create JavaFx project and I need to create invoice design using JasperReports library when I download JasperReports project from SourceForge website the extracted folder don't contain the dist folder which contains the required jar files to build the invoice design the folder contains this folder, what's wrong here "I'm new with JasperReports"


Answer:

Since you're using Maven, JasperReports have a Maven dependency that you can use instead of manually downloading and adding the binaries. You can find the dependency here. The latest version available:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.12.2</version>
</dependency>

Question:

Trying to perform some printings using JasperReport.

JasperReport provides a function to print a document to a BufferedImage, which i convert to a WritableImage to display it on an ImageView object (as shown in the code below).

By time i get a Java Heap space out of memory exception in the getImage(int pageNumber) function. My guess is that the old references to the images are not freed.

Is it possible to fix that ?

private void viewPage(int pageNumber) throws JRException {
    this.resultViewer.setFitHeight(this.imageHeight * this.zoomFactor);
    this.resultViewer.setFitWidth(this.imageWidth * this.zoomFactor);
    this.resultViewer.setImage(this.getImage(pageNumber));
}

@FXML
private ImageView resultViewer;

private WritableImage getImage(int pageNumber) throws JRException {
    return SwingFXUtils
            .toFXImage((BufferedImage) JasperPrintManager.printPageToImage(this.jasperPrint, pageNumber, 2), null);
}

Answer:

I fixed the problem by caling flush() function on the last displayed BufferedImage before displaying a next one

Question:

I have 2 projects which run JasperReports. One which runs on JavaFX and one with just plain Java and a main class. Now, if I ran the plain Java project jasperReport works and displays (vai JasperViewer class), but with the JavaFX project it works but no report displays after clicking the button.

Why does it work for plain Java class but not for JavaFX? What can I do to make it work in JavaFX?

Everything else is the same aside from them being javaFx and plain Java. I downloaded the following jar files for this:

  • Dynamic Jasper 5.0.5. jar
  • jasperreports 5.6.0.jar
  • commons-logging 1.2.jar
  • commons beanutils 1.9.2.jar
  • commons digester 2.1.jar

    commons collections 3.2.2.jar

Here are the projects:

NO.1. JavaFX:

DynaReportApp.java class

package jasperreport;

import java.util.ArrayList;
import java.util.Collection;
public class DynaReportApp {
    public DynaReportApp() {

    }
    public EmployeeReport start() {
        Collection<Employee> list = new ArrayList<>();

        list.add(new Employee(101, "Ravinder Shah",  67000, (float) 2.5));
        list.add(new Employee(102, "John Smith",  921436, (float) 9.5));
        list.add(new Employee(103, "Kenneth Johnson",  73545, (float) 1.5));
        list.add(new Employee(104, "John Travolta",  43988, (float) 0.5));
        list.add(new Employee(105, "Peter Parker",  93877, (float) 3.5));
        list.add(new Employee(106, "Leonhard Euler",  72000, (float) 2.3));
        list.add(new Employee(107, "William Shakespeare",  33000, (float) 1.4));
        list.add(new Employee(108, "Arup Bindal",  92000, (float) 6.2));
        list.add(new Employee(109, "Arin Kohfman",  55000, (float) 8.5));
        list.add(new Employee(110, "Albert Einstein",  89000, (float) 8.2));

        EmployeeReport report = new EmployeeReport(list);
        return report;

    }
}

Employee.java class

package jasperreport;

public class Employee {
    private int empNo;
    private String name;    
    private int salary;
    private float commission;

    public Employee() {
    }

    public Employee(int empNo, String name, int salary, float commission) {
        this.empNo = empNo;
        this.name = name;        
        this.salary = salary;
        this.commission = salary*commission;
    }

    public int getEmpNo() {
        return empNo;
    }

    public void setEmpNo(int empNo) {
        this.empNo = empNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public float getCommission() {
        return commission;
    }

    public void setCommission(float commission) {
        this.commission = commission;
    }
}

EmployeeReport.java class

package jasperreport;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.StyleBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class EmployeeReport {
    private final Collection<Employee> list = new ArrayList<>();

    public EmployeeReport(Collection<Employee> c) {
        list.addAll(c);
    }

    public JasperPrint getReport() throws ColumnBuilderException, JRException, ClassNotFoundException {
        Style headerStyle = createHeaderStyle();
        Style detailTextStyle = createDetailTextStyle();        
        Style detailNumberStyle = createDetailNumberStyle();        
        DynamicReport dynaReport = getReport(headerStyle, detailTextStyle,detailNumberStyle);
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynaReport, new ClassicLayoutManager(), new JRBeanCollectionDataSource(list));
        return jp;
    }

    private Style createHeaderStyle() {        
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM_BOLD);
        sb.setBorder(Border.THIN());
        sb.setBorderBottom(Border.PEN_2_POINT());
        sb.setBorderColor(Color.BLACK);
        sb.setBackgroundColor(Color.ORANGE);
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.CENTER);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setTransparency(Transparency.OPAQUE);        
        return sb.build();
    }

    private Style createDetailTextStyle(){
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM);
        sb.setBorder(Border.DOTTED());        
        sb.setBorderColor(Color.BLACK);        
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.LEFT);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setPaddingLeft(5);        
        return sb.build();
    }

      private Style createDetailNumberStyle(){
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM);
        sb.setBorder(Border.DOTTED());        
        sb.setBorderColor(Color.BLACK);        
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.RIGHT);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setPaddingRight(5);        
        return sb.build();
    }



    private AbstractColumn createColumn(String property, Class type,
            String title, int width, Style headerStyle, Style detailStyle)
            throws ColumnBuilderException {
        AbstractColumn columnState = ColumnBuilder.getNew()
                .setColumnProperty(property, type.getName()).setTitle(
                        title).setWidth(Integer.valueOf(width))
                .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
        return columnState;
    }

    private DynamicReport getReport(Style headerStyle, Style detailTextStyle, Style detailNumStyle) throws ColumnBuilderException, ClassNotFoundException {

        DynamicReportBuilder report=new DynamicReportBuilder();

        AbstractColumn columnEmpNo = createColumn("empNo", Integer.class,"Employee Number", 30, headerStyle, detailTextStyle);
        AbstractColumn columnName = createColumn("name", String.class,"Name", 30, headerStyle, detailTextStyle);        
        AbstractColumn columnSalary = createColumn("salary", Integer.class,"Salary", 30, headerStyle, detailNumStyle);
        AbstractColumn columnCommission = createColumn("commission", Float.class,"Commission", 30, headerStyle, detailNumStyle);
        report.addColumn(columnEmpNo)
                .addColumn(columnName).addColumn(columnSalary).addColumn(columnCommission);

        StyleBuilder titleStyle=new StyleBuilder(true);
        titleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        titleStyle.setFont(new Font(20, Font._FONT_GEORGIA, true));

        StyleBuilder subTitleStyle=new StyleBuilder(true);
        subTitleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        subTitleStyle.setFont(new Font(Font.MEDIUM, Font._FONT_GEORGIA, true));



        report.setTitle("Employee Report");
        report.setTitleStyle(titleStyle.build());
        report.setSubtitle("Commission received by Employee");
        report.setSubtitleStyle(subTitleStyle.build());
        report.setUseFullPageWidth(true); 
        return report.build();
    }     
}

JasperReport.java

package jasperreport;

import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReport extends Application {

    @Override
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();   
        Button button = new Button("Button");
        button.setOnAction(event -> {
            try {
                DynaReportApp dynaReportApp = new DynaReportApp();
                JasperPrint jp = dynaReportApp.start().getReport();
                JasperViewer jasperViewer = new JasperViewer(jp);
                jasperViewer.setVisible(true);
                JasperViewer.viewReport(jp, false);

            } catch (JRException | ColumnBuilderException | ClassNotFoundException ex) {
                System.out.println( ex);
            }
        });



        root.getChildren().addAll(button);

        Scene scene = new Scene(root, 400, 250);

        primaryStage.setTitle("JasperReport");
        primaryStage.setScene(scene);
        primaryStage.show();         
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    } 
}

NO.2. Ordinary Java

Employee.java

package jasperreporttest;

public class Employee {
    private int empNo;
    private String name;    
    private int salary;
    private float commission;

    public Employee() {
    }

    public Employee(int empNo, String name, int salary, float commission) {
        this.empNo = empNo;
        this.name = name;        
        this.salary = salary;
        this.commission = salary*commission;
    }

    public int getEmpNo() {
        return empNo;
    }

    public void setEmpNo(int empNo) {
        this.empNo = empNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public float getCommission() {
        return commission;
    }

    public void setCommission(float commission) {
        this.commission = commission;
    }
}

EmployeeReport.java

package jasperreporttest;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.StyleBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class EmployeeReport {
    private final Collection<Employee> list = new ArrayList<>();

    public EmployeeReport(Collection<Employee> c) {
        list.addAll(c);
    }

    public JasperPrint getReport() throws ColumnBuilderException, JRException, ClassNotFoundException {
        Style headerStyle = createHeaderStyle();
        Style detailTextStyle = createDetailTextStyle();        
        Style detailNumberStyle = createDetailNumberStyle();        
        DynamicReport dynaReport = getReport(headerStyle, detailTextStyle,detailNumberStyle);
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynaReport, new ClassicLayoutManager(), new JRBeanCollectionDataSource(list));
        return jp;
    }

    private Style createHeaderStyle() {        
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM_BOLD);
        sb.setBorder(Border.THIN());
        sb.setBorderBottom(Border.PEN_2_POINT());
        sb.setBorderColor(Color.BLACK);
        sb.setBackgroundColor(Color.ORANGE);
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.CENTER);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setTransparency(Transparency.OPAQUE);        
        return sb.build();
    }

    private Style createDetailTextStyle(){
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM);
        sb.setBorder(Border.DOTTED());        
        sb.setBorderColor(Color.BLACK);        
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.LEFT);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setPaddingLeft(5);        
        return sb.build();
    }

      private Style createDetailNumberStyle(){
        StyleBuilder sb=new StyleBuilder(true);
        sb.setFont(Font.VERDANA_MEDIUM);
        sb.setBorder(Border.DOTTED());        
        sb.setBorderColor(Color.BLACK);        
        sb.setTextColor(Color.BLACK);
        sb.setHorizontalAlign(HorizontalAlign.RIGHT);
        sb.setVerticalAlign(VerticalAlign.MIDDLE);
        sb.setPaddingRight(5);        
        return sb.build();
    }



    private AbstractColumn createColumn(String property, Class type,
            String title, int width, Style headerStyle, Style detailStyle)
            throws ColumnBuilderException {
        AbstractColumn columnState = ColumnBuilder.getNew()
                .setColumnProperty(property, type.getName()).setTitle(
                        title).setWidth(Integer.valueOf(width))
                .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
        return columnState;
    }

    private DynamicReport getReport(Style headerStyle, Style detailTextStyle, Style detailNumStyle) throws ColumnBuilderException, ClassNotFoundException {

        DynamicReportBuilder report=new DynamicReportBuilder();

        AbstractColumn columnEmpNo = createColumn("empNo", Integer.class,"Employee Number", 30, headerStyle, detailTextStyle);
        AbstractColumn columnName = createColumn("name", String.class,"Name", 30, headerStyle, detailTextStyle);        
        AbstractColumn columnSalary = createColumn("salary", Integer.class,"Salary", 30, headerStyle, detailNumStyle);
        AbstractColumn columnCommission = createColumn("commission", Float.class,"Commission", 30, headerStyle, detailNumStyle);
        report.addColumn(columnEmpNo)
                .addColumn(columnName).addColumn(columnSalary).addColumn(columnCommission);

        StyleBuilder titleStyle=new StyleBuilder(true);
        titleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        titleStyle.setFont(new Font(20, Font._FONT_GEORGIA, true));

        StyleBuilder subTitleStyle=new StyleBuilder(true);
        subTitleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        subTitleStyle.setFont(new Font(Font.MEDIUM, Font._FONT_GEORGIA, true));



        report.setTitle("Employee Report");
        report.setTitleStyle(titleStyle.build());
        report.setSubtitle("Commission received by Employee");
        report.setSubtitleStyle(subTitleStyle.build());
        report.setUseFullPageWidth(true); 
        return report.build();
    }     
}

JasperReportTest

package jasperreporttest;

import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import java.util.ArrayList;
import java.util.Collection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReportTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Collection<Employee> list = new ArrayList<>();

        list.add(new Employee(101, "Ravinder Shah",  67000, (float) 2.5));
        list.add(new Employee(102, "John Smith",  921436, (float) 9.5));
        list.add(new Employee(103, "Kenneth Johnson",  73545, (float) 1.5));
        list.add(new Employee(104, "John Travolta",  43988, (float) 0.5));
        list.add(new Employee(105, "Peter Parker",  93877, (float) 3.5));
        list.add(new Employee(106, "Leonhard Euler",  72000, (float) 2.3));
        list.add(new Employee(107, "William Shakespeare",  33000, (float) 1.4));
        list.add(new Employee(108, "Arup Bindal",  92000, (float) 6.2));
        list.add(new Employee(109, "Arin Kohfman",  55000, (float) 8.5));
        list.add(new Employee(110, "Albert Einstein",  89000, (float) 8.2));

        EmployeeReport report = new EmployeeReport(list);

        try {
            JasperPrint jp = report.getReport();
            JasperViewer jasperViewer = new JasperViewer(jp);
            jasperViewer.setVisible(true);

        } catch (JRException | ColumnBuilderException | ClassNotFoundException ex) {

        }
    } 
}

Answer:

All right, this has been one heck of a roller-coaster but I finally found my solution and I hope it will help others as well. So apparently the above approach is correct. Everything is right to the dot. The only thing missing is jasper-compliler-jdlt 5.5.23.jar. So as long as you have all the above jars it should work. I also noticed that for some reason if you use a higher DynamicJasper version like 5.1.1, the whole thing won't work. So just try to keep the following jar files(list of complete jar files can be found below) as well as the above implementation and you should be fine. Cheers and happy coding :)

List of jar files

DynamicJasper 5.0.5. jar
commons-beanutils 1.9.2.jar
commons-collections 3.2.2.jar
commons-digester 2.1.jar
commons-logging 1.2.jar
jasperreports 5.6.0.jar or jasperreports 6.0.0.ar
jasper-compliler-jdlt 5.5.23.jar

Question:

I want to create report with Jasper on JavaFX. I export data from MySql into the Jasper Report and everything work correct. I import library Jasper Reports 6.2.0 to my project library on Netbeans 8.2. When i want to create method (in my case showReport()) to open Jasper File when one button is clicked, it show me error cnnot find symbol on compileReport() method and fillReport()`method.

here is also the picture of code!! enter image description here ?

code is here:

public void showReport(){
    try{
        JasperReport jasperReport = new JasperCompileManager.compileReport(" C:\\Users\\PC\\Desktop\\fxmlTest\\src\\fxmltest\\newReport.jasper");
        JasperPrint jasperPrint   = new JasperFillManager.fillReport(jasperReport,null,connection);      
        JRViewer viewer  = new JRViewer(jasperPrint);
        viewer.setOpaque(true);
        viewer.setVisible(true);

        this.add(viewer);
        this.setSize(900,500);
        this.setVisible(true);
    }catch(Exception e){
        System.out.println( e.getMessage());
    }
};

Answer:

JasperCompileManager.compileReport and JasperFillManager.fillReport are static methods.

The syntax you're using on the right hand sides of the assignments

new JasperCompileManager.compileReport(" C:\\Users\\PC\\Desktop\\fxmlTest\\src\\fxmltest\\newReport.jasper")
new JasperFillManager.fillReport(jasperReport,null,connection)

tells the compiler to invoke the constructors of the static inner class compileReport in JasperCompileManager and fillReport in JasperFillManager respectively. (Or alternatively the constructors of the classes fillReport in the JasperCompileManager package and fillReport in the JasperFillManager package.) However those classes don't exist which is why you get the compile time error. You need to remove those new keywords:

JasperReport jasperReport = JasperCompileManager.compileReport(" C:\\Users\\PC\\Desktop\\fxmlTest\\src\\fxmltest\\newReport.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,connection);