ICL Sales EXIMON ICL Sales EXIMON - 4 months ago 42
Java Question

Displaying report from jFrame by jButton click event

I am using NetBeans 8.0.2. I have already created report using iReport 5.6.0. After designing I moved it into my existing Java Application project in NetBeans into new folder called reports.

My jFrame file reportform.java is in same project under appfolder. I wish to display/preview my already created report by click of a Button on jFrame reportform.java. My report is working/displaying fine otherwise in the same project.

Answer
            String sfilename="report";
    String path = "appname/" + sfilename + ".jasper";
    String imgp=("images");

    InputStream reportName = this.getClass().getClassLoader().getResourceAsStream(path);

    HashMap param = new HashMap();
    param.put("param1","value1");
    param.put("param2","value2");
    param.put("imgpath",imgp); 
    param.put("SUBREPORT_DIR", "appname/"); 

    try {   
        JasperPrint jp = JasperFillManager.fillReport(reportName, param, conn);

        net.sf.jasperreports.swing.JRViewer jv = new net.sf.jasperreports.swing.JRViewer(jp);

 //Insert viewer to a JFrame to make it showable
        JFrame jf = new JFrame();
        jf.getContentPane().add(jv);
        jf.validate();
        jf.setVisible(true);
        jf.setSize(new Dimension(800,600));
        jf.setLocation(300,100);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setTitle("JASPER REPORT");