Menu

HOME > SUPPORT > SWING VIEWING AND PRINTING

Swing Viewing and Printing

 

Overview

The majority of ReportMill developers use ReportMill in browser based, client-server applications. However, it is not uncommon to use ReportMill from a Swing based desktop application. In this context, it is often necessary to provide the user a means of viewing and printing reports. There are two options for this. One is to use the platform PDF viewer and the second is to use ReportMill's RMViewer component.

Using the Platform PDF Viewer

Using the platform PDF viewer is often the easiest and most fluid way to allow users to view and print generated reports. ReportMill itself does this for its "preview reports" feature. We do this with the following open method, which works for Macintosh and Windows:

public static void openFile(String aName)
{
  boolean isWin = System.getProperty("os.name").indexOf("Windows") >= 0;
  String command = (isWin? "explorer " : "open ") + aName;
  try { Runtime.getRuntime().exec(command); }
  catch(Exception e) { e.printStackTrace(); }
}

Using the platform PDF viewer is not only easy, it also offers several benefits: it presents a document using the standard interface generally already familiar to the user, it has native platform performance, it generally has some document manipulation features - such as page rotation/scale and image and text copy/paste and it generally has advanced native platform printing integration.

Using ReportMill's RMViewer Component

Having said all that, using ReportMill's viewer component is also easy and can sometimes provide a more integrated report viewing experience. Documentation for RMViewer is available in our javadocs, however, most developers simply need to invoke code similar to this:

// Import Viewer package and base
import com.reportmill.*;
import com.reportmill.base.*;

// Create new viewer and set document
RMViewer viewer = new RMViewer();
viewer.setDocument(myReport);

// Create new scrollpane for viewer and install in a window

JScrollPane scrollPane = new JScrollPane(viewer);
myFrame.getContentPane().add(scrollPane, Border.CENTER);

It's also easy to perform direct printing with RMViewer:

new RMViewer(aDocument).print();

An Example

We've implemented a nice example of using ReportMill's RMViewer in a simple Swing application. Although most ReportMill applications take advantage of the powerful layout application, this example is a simple port of our web based AdHoc example and thus generates a template dynamically based on user selected attributes. This example is available here:

    http://reportmill.com/examples/adhoc