JasperViewer it is a very useful component which allows you to preview the reports from your application before saving them  as documents. It also offers additional  functionality like printing the reports, saving them with different extensions (ODT, HTML, DOC, PDF and others), navigating through out the report’s pages and zooming in or out.  This was the perfect component for fulfilling the customers request here at RationalPlan because it was very easy and straightforward to use. The tricky part and also what I consider a drawback is that if you want to customize its appearance and functionality you can not do it.

First let us try using the viewer as it is without any customizations. The class that describes the viewer is JasperViewer. Looking at the API you can see that the constructors can receive three types of components: an InputStream, a JasperViewer or a String that describes the source file. Because we use a custom data source (click to see the tutorial) to fill our reports it was very simply for us to use the already generated JasperPrint object and pass it to the JasperViewer constructor.

[sourcecode language=”java”]
JasperPrint print = JasperFillManager.fillReport(projectCalendarStream, hm, mainReportDS);
JReportsViewer jReportsViewer = new JReportsViewer(print);
jReportsViewer.viewReport(); [/sourcecode]

In the first line of code we created the JasperPrint component with the JasperFillManager and then we pass it to the constructor in order to create the report. The last line just shows up the JasperViewer dialog.

The next step is to create a customized version of the JasperViewer component to fulfill our needs. Let’s say we want to allow the users to save the reports only as a PDF document. Here is the source code for JasperReports.

[sourcecode language=”java”]
public JReportsViewer(JasperPrint jasperPrint, boolean isExitOnClose, Locale locale) {
if (locale != null) {
this.isExitOnClose = false;


this.viewer = new JRViewer(jasperPrint, locale);
List<JRSaveContributor> newSaveContributors = new LinkedList<JRSaveContributor>();
JRSaveContributor[] saveContributors = this.viewer.getSaveContributors();
for (int i = 0; i < saveContributors.length; i++) {
if (saveContributors[i] instanceof JRPdfSaveContributor) {
this.viewer.setSaveContributors(newSaveContributors.toArray(new JRSaveContributor[0]));
this.pnlMain.add(this.viewer, BorderLayout.CENTER);

This is one of the constructors that we kept because we use a JasperPrint object. We have deleted the other ones because we don’t use source files or InputStreams. As you can see inside this constructor we have a list of JRSaveContributors. These contributors represent the save extensions and as already mentioned we only want to keep the PDF format. To achieve this we look for the JRPdfSaveContributor and when we find it we add it to our list of contributors. Then we set this list as the new list of contributors.

This is a minor customization but if you want other complex ones you might need to take the source code, apply your changes, create the jar and then replace the original JasperReports jar with your own.

Hope this short tutorial helps. Good luck!