Java print package and printing API
Describes the interface for printing and explains how to use the main class in the printing API to print a component.
Explains the features of the printing framework.
Describes the method in the printable interface.
Explains how to create a printer job for a component and then print it.
Overview
The printing framework is implemented by using the printing APIs which have been available since JDK™ 1.3 and JDK 1.4.
The printable interface
The Printable interface is the most important interface of the java.awt.print package. To make a UI component printable, you need to implement this interface, which has a single method:
public int print(Graphics graphics,
PageFormat pageFormat,
int pageIndex) throws PrinterException
This method is called by the Java™ printing system when it sends the printing instructions to a printer. To print your component, you need to call the drawing methods of the Graphics object passed as the first argument.
While printing your component, you can get more information on the type of paper being currently used from the second and the third arguments of this method.
After implementing the Printable interface, you need to create a PrinterJob object to print your component.
Using the PrinterJob class to print a component
The PrinterJob class is the root of the java.awt.print package. To print your component, you must create a PrinterJob object. The PrinterJob class has a static method called getPrinterJob().
1. Call method getPrinterJob() to create a printer job.
2. Call the setPrintable(Printable painter) method to specify the Printable object you have implemented.
3. To change the printer parameters you can open the Print dialog box by calling the printDialog() method.
4. Print dialog box of Java SE
5. If you want to change the page format, open the Page Setup dialog box by calling the pageDialog() method.
6. Page Setup Dialog Box of Java SE
7. To send the printing job to the selected printers, call the print() method of the printer job.
See the documentation of the PrinterJob class for more information.
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.