Over a million developers have joined DZone.

Print JasperReport Without A DataSource

DZone's Guide to

Print JasperReport Without A DataSource

Free Resource
This is a method that fills a Jasper Reports .jasper file using only parameters.  No Data Source is needed.  Note: "module" is an object whose fields are used as the parameters to the report.  The method also calls JasperViewer.viewReport which starts the embedded Jasper Reports viewer.  This could be replaced with a method to export the report or anything else.

private void printCertsActionPerformed(java.awt.event.ActionEvent evt) {                                           
        File reportFile = new File("report1.jasper");
        Map parameters = new HashMap();
        parameters.put("partNum", module.getPartNum());
        parameters.put("moduleType", module.getModuleType());
        parameters.put("area", module.getArea());
        parameters.put("membraneType", module.getMembraneMaterial());
        parameters.put("channelHeight", module.getChannelHeight());
        parameters.put("controlNum", cnField.getText());
        try {
            JasperPrint print = JasperFillManager.fillReport(reportFile.getPath(), parameters, new JREmptyDataSource());
        } catch (JRException ex) {
            Logger.getLogger(TestDashboardView.class.getName()).log(Level.SEVERE, null, ex);

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}