Monday, July 13, 2015

using ireport in javafx applications



one of reporting solutions in java is to use ireports from JasperSoft. here we choose to generate a xhtml report and send it to a web view component. 
you must now that ireport wont start under java8 you must have jdk 7 or under installed .
go to directory of ireport installation and edit the file ireport.conf which is in the  etc folder. uncomment the line jdkhome and make it point to the older jdk.


first we need of course to create a report. this one is simple it's a result of the simple query "select * from emp"  with the schema scott in oracle database.  make a connection and use the wizard for this simple report : 






next we take the resulting scott.jrxml into our new netbeans project :

 to setup a ireport project we must include the following jars :
  • commons-logging.
  • commons-digesters
  • commons-collections.
  • commons-beanutils.
  • groovy-all
  • jasperreports-5.6.0
  •  jasperreports-fonts-5.6.0
  •  jasperreports-htmlcomponent-5.0.

Our goal is to generate a xhtml report and injecting into the Webview component.

to generate the report we use ReportPrinter class :
 



package ireportfx;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
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.export.JRXhtmlExporter;
        
/**
 *
 * @author hakim
 */
public class ReportPrinter {

    Connection conn = null;

    public ReportPrinter(Connection conn) {
        this.conn = conn;
    }


    private String jasperReport(String name, 
            ResultSet data, Map params) {

        JasperPrint jasperPrint = null;
        try {
            JasperReport jr=JasperCompileManager.compileReport(ReportPrinter.class.getResourceAsStream(name));
            InputStream stream = ReportPrinter.class.getResourceAsStream(name);
            jasperPrint = JasperFillManager.fillReport(
                    jr, params, conn);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(ReportPrinter.class.getName()).log(Level.SEVERE, null, ex);
        }

        JRExporter exporter = null;
        StringWriter out = new StringWriter();
        PrintWriter printWriter = new PrintWriter(out);

        try {
            exporter = new JRXhtmlExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT,
                    jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
                    printWriter);

        } catch (RuntimeException e) {
            throw e;
        }
        try {
            exporter.exportReport();
        } catch (JRException ex) {
            Logger.getLogger(ReportPrinter.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(ReportPrinter.class.getName()).log(Level.SEVERE, null, ex);
        }

        return out.toString();
    }

    public String generateReport(String ReportName, Map params) {
        return jasperReport(ReportName, null, params);
    }
    
}


and this is the simple controller that fill the report :
 
package ireportfx;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.web.WebView;

/**
 *
 * @author hakim
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    WebView webView;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        webView.getEngine().loadContent(generateReport());
    }


    Connection getConnection()  {
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        Connection con=null;
        try {
            con = DriverManager.getConnection(url,"scott","tiger");
        } catch (Exception except) {
            except.printStackTrace();

        }
        return con;
    }

    private String generateReport() {
          ReportPrinter rp=new ReportPrinter(getConnection())  ;
          String sp=rp.generateReport("scott.jrxml",  null);
          System.out.println(sp);
          return sp;
    }

}

the result is in the screenshot below :


as usual you can find the full source code at git hub :

https://github.com/mooninvader/ireportfx/tree/master/src/ireportfx


7 comments:


  1. Thanks for post.I do agree your blog for quiz programming concepts, which is very helpful to grow up your knowledge.

    aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery

    ReplyDelete
  2. This post is so interactive and informative.keep update more information...
    AWS Training in Velachery
    AWS Training in Chennai

    ReplyDelete