Search code examples
javajasper-reports

JasperViewer - How to set file name and set the extension (format)?


I am developing Java desktop based application using Swing framework and JasperReports for reporting. I have some problems, when JasperViewer comes up and I want to save the report.

How can I set the name of the file (default is blank) in the save dialog and how to limit only Pdf format?

Here is my code:

JRBeanCollectionDataSource beanCollection=new JRBeanCollectionDataSource(matakuliahs);

Map<String,Object> map=new HashMap<String, Object>();
map.put("namaMahasiswa", mahasiswa.getNama());

InputStream input=getClass().getResourceAsStream("/reportsederhana/reportsederhana.jrxml");
JasperDesign design=JRXmlLoader.load(input);
JasperReport report=JasperCompileManager.compileReport(design);
JasperPrint jasperPrint=JasperFillManager.fillReport(report, map, beanCollection);
jasperPrint.setName("Laporan "+mahasiswa.getNama()); // it doesn't give the name
JasperViewer.viewReport(jasperPrint,false);

Solution

  • For setting the default extension(s) to save to, one idea is that you need to somehow get the JRViewer instance from a JasperViewer (instance) and then, on the void JRViewer instance, you can set some save contributions. Have a look at JRViewer.setSaveContributors(JRSaveContributor[] saveContribs).

    For setting the file name, i am not entirely sure, but have a look at JRViewer.JRViewer(String, boolean).

    Also check the JRViewer and the JasperViewer source code, it may come in handy.