JClass LiveTable

PreviousNextIndex

8

Table Printing

Printing   Print Preview

Although JClass LiveTable is a grid/table component, it still allows end users to print and print-preview table applications. By using the JCPrintTable class, you can control layout, formatting, and header/footer information.


8.1 Printing

The JCPrintTable class offers print functionality in JCTable. The following code creates a JCPrintTable and prints JCTable with the default print options:

  JCPrintTable pt = new JCPrintTable(myTable);
  pt.print();

Default printed pages consist of:

The JCPrintTable class creates a copy of the table's visible properties and retrieves cell contents from the data source. Cell height and width are copied by actual pixel size. Scrollbars are not part of the printed table.

Using JCPrintTable offers controls over various aspects of your printed pages.

8.1.1 Setting Page Layout Properties

The JCPrintTable class provides methods for detailed control of print output from a JClass LiveTable application or applet.

Page Size

The following methods define printed page sizes in pixels:

  setPageDimensions();
  setPageWidth();
  setPageHeight();

Use the getPageDimensions(), getPageWidth(), and getPageHeight() methods to retrieve page sizes by retrieving page information from the printer. By default, the standard A4 page (8½" x 11") is used.

Page Margins

Page margins are set using the setPageMargins() method. This method uses the java.awt.Insets class to set the margins as in the following example:

  printtable.setPageMargins(new Insets(54,36,36,54));

By default, using the Insets object to respectively specify top, left, bottom and right insets will set the margins in pixels. To specify margin units in inches, use the variable MARGIN_IN_INCHES in the getMarginUnits() method:

  setMarginUnits(JCPrintTable.MARGIN_IN_INCHES);

You can retrieve page margins based on the Insets of the page using the getPageMargins() method. Use the getDefaultPageMargins() to retrieve the default Insets.

Page Numbering

To control page numbering, use getNumHorizontalPages() and getNumVerticalPages() to determine the number of pages across and down. Use getNumPages() to determine the total number of pages required to print the table, based on how you have defined the page and margin sizes.

8.1.2 Page Resolution

Use the getPageResolution() method to get the printer page resolution. The default is 72 pixels per inch. Use setPageResolution() to set the printer page resolution.

8.1.3 Printing Headers and Footers

Headers and footers are applied using JCPrintListener receiving JCPrintEvent events. A JCPrintEvent is posted for each page during printing, and provides a graphic object clipped to the allowable paint region, the page number of the current page, and the total number of pages:

  public JCPrintEvent
    (Table table, Graphics gc, int page, intnumPages, int Type);
  public Graphics getGraphics();
  public Insets getPageMargins();
  public int getMarginUnits();
  public int getNumHorizontalPages();
  public int getNumPages();
  public int getNumVerticalPages();
  public int getPage();
  public Dimension getPageDimensions();
  public int getPageResolution();
  public Dimension getTableDimensions();

getTableDimensions() can be used in the printPageBody() method of JCPrintListener() to determine the size the table occupies on the page.

The JCPrintListener requires that three methods are defined:

  public void printPageHeader(JCPrintEvent e);
  public void printPageFooter(JCPrintEvent e);
  public void printPageBody(JCPrintEvent e);

The printPageBody method is called after JClass LiveTable has finished setting up the print of the page body, but just before it is actually sent to the printer.

The following code produces the footer illustrated below:

  public void printPageFooter(JCPrintEvent e) {
    Graphics gc = e.getGraphics();  
    Rectangle r = gc.getClipRect();

    FontMetrics fm = gc.getFontMetrics();

    String page = "Page " + e.getPage();
    String note = "Use JCPrintListener to customize the footer!";
  // Pad the footer text to the right
    gc.drawString(page, 0, r.height/2);
    gc.drawString(note, r.width - fm.stringWidth(note), r.height/2);
  }

Figure 17 :  A Page Footer.

If you don't register a JCPrintListener for the table, the print engine will default to printing a centered footer containing the text Page x, where x is the page number. If you do register a JCPrintListener, however, then you are responsible for the placing the page number either in the header or footer of the page.


8.2 Print Preview

Using JCPrintPreview

JClass LiveTable provides a class that displays a preview of the print job in a separate frame. Using the print preview frame, end-users can flip through the pages of the print job, and send the current page or all of the pages to the printer.

To add the print preview functionality, use JCPrintPreview:

  JCPrintPreview(String title, JCPrintTable table)
  showPage(int page)

For example, the following provides a preview beginning at the first page of the job:

  JCPrintPreview pf = new JCPrintPreview("Table Print Preview",printtable);
  pf.showPage(0);

Using JCPrintTable

Alternatively, to allow users to preview a print job, you can use JCPrintTable's showPrintPreview() method.

An example of print preview exists in the PrimeTime.java demo, located in the demos/table/primetime directory.


Figure 18 :  The JClass LiveTable Print Preview Window.


PreviousNextIndex