Java API-Based Printing

First we will address printing tables in reports. Using a very simple example we will describe the process for generating printed output containing a table.

The Previewer is a frame component that can be used to preview a report. The previewer is Java2 based and provides support for Java2 printing API.

In this chapter we will also discuss table styles. We also describe the use of the various table adapters, which allow for the printing of table based components or data structures automatically. Table adapters also completely eliminate the need for an application to manually setup the table in a report.

demo icon
View a 2-minute demonstration of InetSoft's easy, agile, and robust BI software.

Printing Tables

There are two implementations for a ReportSheet: a StyleSheet or a TabularSheet. The StyleSheet represents the flow based layout model and a TabularSheet represents the tabular grid based layout model. The next step is to cast the ReportSheet to a TabularSheet or a StyleSheet depending on whether you wish to have a tabular or flow based layout method.

StyleSheet layout = new StyleSheet(); 

The report is empty when it is first created. You must add document elements to the report to set up the report. Elements accepted by the report include table, chart, image, text and more.

To add a table to the report, we first need to obtain a TableLens object. Assuming a Swing JTable is displayed on screen, you can simply use the Swing table adapter in the inetsoft.report.lens.swing package to get a TableLens object:

layout.addTable(new JTableLens(jtable)); 

A table lens is a generic interface for defining the data source for tabular data. In this example, we use an on-screen table as the data source. It is just as easy to create a data source from databases or second-tier servers. Since the focus of this document is report formatting and layout, we will use on-screen tables in these examples and will briefly review the steps for creating other data feeds later.

To print the report, simply call the print() method in the ReportSheet.

report.print(); 

If we are exporting the report to another format, or using the custom driver, we get a java.awt.PrintJob and then print the report. The export classes and the custom driver provide methods to get the PrintJob.

PrintJob job = ...; Report.print(job); 

These two lines of code will cause a report that contains the same table on the screen to be printed. This is the simplest form of using Style Intelligence to generate printed copies of tables.

Example: Printing JTable

We are going to use this example as the base and add more advanced styles and features to create a complete report example.

Listing 7. Printing JTable (Report1.java)

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import inetsoft.report.*;
import inetsoft.report.lens.swing.*;
 
/**
* This example shows how to print a JTable (swing).
*
* @version 1.2.12.1, 5/26/2009
* @author InetSoft Technology Corp
*/

public class Report1 extends Frame {
   public Report1() {
      super("JTable Report");
      final String header[] =
        {"Company","Close","P/E","Yield", "ROE"};
      final String[][] data = {
        {"ADE CORPORATION", "8.38", "13.7", "0.0", "4.8"},
        {"AEHR TEST SYSTEMS", "3.63", "10.1", "0.0", "6.0"},
        {"AG ASSOCIATES INC", "2.75", "NE", "0.0", "NE"},
        ...
        {"RELIABILITY INC", "4.47", "3.3", "0.0", "39.3"},
        {"SEMITOOL INC", "6.44", "8.9", "0.0", "15.3"},
      };

      // create testing data
      TableModel model = new AbstractTableModel() {
      public Object getValueAt(int r, int c) {
         if (r >= data.length) {
         return null;
      }
      if (c >= data[r].length) {
         return null;
      }
 
      return data[r][c];
   }
   public int getRowCount() { return data.length; }
   public int getColumnCount() { return data[0].length; }
   public String getColumnName(int c) { return header[c]; }
   public Class getColumnClass(int c) {return String.class;}
};
 
table = new JTable(model);
add(new JScrollPane(table), "Center");
Panel pnl = new Panel();
add(pnl, "South");
Button printB = new Button("Print");
pnl.add(printB);
printB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ReportSheet report = createReport();
try {
report.print();
}
catch(Exception x) {}
}
});
Button quitB = new Button("Quit");
pnl.add(quitB);
quitB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public ReportSheet createReport() {

StyleSheet layout = new StyleSheet();
 
// create a table lens
JTableLens lens = new JTableLens(table);
 
layout.addTable(lens);
return layout;
}
private JTable table;
public static void main(String[] args) {
      ReportEnv.setProperty("StyleReport.useCustomDriver", "true");
      Report1 win = new Report1();
      win.pack();
      win.setVisible(true);
}
}