Parametrized IReport in Your Java Project ?

This is a very important post I've to make during this series about IReports which is to enlighten somebody who reads this to add Parametrized IReport into the Java Project.

The whole post in steps which will surely be comforting to most of the readers, so that it'll be easy to follow this.

1. First, Step is to finalize your IReport. Hope You've got your IReport Application Opened. Then, click on the Parameter You've defined on the Report Inspector. 





 2. Go to Properties window at the right bottom of your application window.








3. Make sure, you tick off 'Use as a prompt'. Compile the IReport & Close IReport Application.

4. Then, open up your Java Project Create a Package as repObj (any name) .



5. Store your IReport files & import *.jasper & image files relating to your IReport in to the package.






6. Create another package 'report' & make a java class as ReportView & copy the following code inside it.


package report;

import DBConnect.Connect;
import java.awt.Container;
import java.sql.Connection;
import java.util.HashMap;
import javax.swing.*;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

public class ReportView extends JFrame
{
    public ReportView(String fileName)
    {
        this(fileName, null);
    }
    public ReportView(String fileName, HashMap para)
    {
        super("Set Your Application name in the Super Class");

        Connect dba = new Connect();
        Connection con = dba.connect();

        try
        {
            JasperPrint print = JasperFillManager.fillReport(fileName, para, con);
            JRViewer viewer = new JRViewer(print);
            Container c = getContentPane();
            c.add(viewer);            
        } 
        catch (Exception Exception)
        {
            System.out.println("ERRORS IN JASPER REPORT "+Exception);
        }
        setBounds(10, 10, 900, 700);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    
}



7. Now, create a button in the frame to fire up your IReport & a text box to pass the parameters.

8. Go inside the Button & copy the following Code.

        HashMap para = new HashMap();
        para.put("Order_No",jtxt_Order_No.getText());
        ReportView rv = new ReportView("..\\iControl\\src\\repObj\\Order.jasper",para);
        rv.setVisible(true);




 You may need to import HashMap class & report.ReportView class( you've created) in your frame *.java file. 

As parameter of put method you've to pass IReport Parameter Name & Text Box Value. ReportView Parametrized Constructor should be given Relative *.jasper path Passed, make sure you do that.

9. Now, you've to import the IReport Libraries in to the Java Project.







Import the  IReport Libraries in the Image below. You can find them in your IReport Installation. In default they'll be "C:\Program Files\Jaspersoft\iReport-4.5.1\ireport\modules\ext".




10. Now, run your file. Type a value into the Text box & Check whether it's working. Most probably it would.


Enjoy !

No comments: