![]() ![]() ![]() |
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 pt = new JCPrintTable(myTable);JCPrintTable
class offers print functionality inJCTable
. The following code creates aJCPrintTable
and printsJCTable
with the default print options:
pt.print();Default printed pages consist of:
- 1" margins
- no header information
- a footer message: page x of y
- all table pages printed (which cannot be changed)
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()
, andgetPageHeight()
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
printtable.setPageMargins(new Insets(54,36,36,54));setPageMargins()
method. This method uses thejava.awt.Insets
class to set the margins as in the following example:By default, using the
setMarginUnits(JCPrintTable.MARGIN_IN_INCHES);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 variableMARGIN_IN_INCHES
in thegetMarginUnits()
method:You can retrieve page margins based on the
Insets
of the page using thegetPageMargins()
method. Use thegetDefaultPageMargins()
to retrieve the defaultInsets
.Page Numbering
To control page numbering, use
getNumHorizontalPages()
andgetNumVerticalPages()
to determine the number of pages across and down. UsegetNumPages()
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. UsesetPageResolution()
to set the printer page resolution.
8.1.3 Printing Headers and Footers
Headers and footers are applied using
public JCPrintEventJCPrintListener
receivingJCPrintEvent
events. AJCPrintEvent
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:
(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 theprintPageBody()
method ofJCPrintListener()
to determine the size the table occupies on the page.The
public void printPageHeader(JCPrintEvent e);JCPrintListener
requires that three methods are defined:
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 aJCPrintListener
, 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(String title, JCPrintTable table)JCPrintPreview
:
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
'sshowPrintPreview()
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.
![]() ![]() ![]() |