|
J2Printer
Basic printing
J2PrinterWorks provides the base class J2Printer for managing the Java printing process. J2Printer implements the complete process for printing a Java Pageable. Pageable is the Java interface for a set of printable pages. The J2Printer class supports such features as headers, footers, and margins, display of page setup, print preview, and print dialogs, printing in a background thread, and detecting print progress events.
J2PrinterWorks provides 7 classes for creating Pageables (each described in their own sections): J2TextPrinter (for JTextPane, JEditorPane, and JTextArea), J2TablePrinter (for JTable), J2TreePrinter (for JTree), J2ListPrinter (for JList), J2PanelPrinter (for JPanel), J2ComponentPrinter (for any Java Component), and J2FlowPrinter (for a sequence of Flowables). For example, the following code creates a Pageable instance for a JTextPane:
J2TextPrinter yourPageable = new J2TextPrinter(yourJTextPane);
You may also create your own Pageables by implementing the Java Pageable interface yourself.
Having created yourPageable, printing it with J2PrinterWorks is accomplished by instantiating a J2Printer instance, giving it your Pageable, and telling it to print, i.e.:
J2Printer printer = new J2Printer();
printer.setPageable(yourPageable);
printer.print();
The J2Printer print method also takes Pageable as an argument, so shorthand alternatives are possible such as:
new J2Printer().print(yourPageable);
or even:
new J2Printer().print(new J2TextPrinter(yourJTextPane));
Multiple Pageables and Books
J2Printer can also print a series of Pageables as a single document using an addPageable method:
J2Printer printer = new J2Printer();
printer.setPageable(pageable1);
printer.addPageable(pageable2);
printer.addPageable(pageable3);
printer.print();
This sequence groups three Pageables together in a single Pageable, implemented using the Java class Book. The result is that the individual pages from the three Pageables will be printed back-to-back in one document.
Having used setPageable and/or addPageable to specify some Pageables, you can use clearPageable() to reset to an empty overall document. Also, note that since the J2Printer instance starts with an empty overall document, you could have used a addPageable in place of the the first setPageable call above.
Printing properties
J2Printer contains a number of methods for controlling the printing process itself. Some of the more commonly used are these:
cancelPrinting (): Cancel the current printing job
setNumberOfCopies (int numberOfCopies): Sets number of copies to be printed.
setPaperSize (double paperW, double paperH): Sets the paper width and height when paper viewed in PORTRAIT.
setPrintDialogUsed (boolean printDialogUsed): Sets whether to show a print dialog before printing.
setPrintJobName (String printJobName): Sets the print job name that appears in the system print monitor.
setSeparatePrintThread (boolean separatePrintThread): Sets whether to print from a separate thread (instead of from the thread that called the print method).
Pageable properties
J2Printer also has methods for setting the headers, footers, margins, orientation, and scale for the Pageable that it will print. The J2Printer values for these define the "global" (default) values used for the overall document, which will be used unless an individual Pageable specifies its own "local" values (see "Global vs. local values" section below).
The following sections describe the individual methods used to control these features.
Headers and footers, boxes and lines
These methods let you define left, center, and right headers and footers. Any of these can be a String specifying a single line in a single font, or a JLabel specifying multiple lines, rich text, and Images. Any of these can contain page numbering and date and time formatting (see Javadoc descriptions for the header and footer methods). Headers and footers can be different on the first page vs. the rest of the pages. Headers and/or footers areas can also be surrounded by a box, separated from the body by a line, or neither of these.
If you wish to have no headers (and/or no footers) and have the body of your document use this space, set the left, center, and right headers (and/or footers) to each be the empty String (""). In this case, J2PrinterWorks will automatically omit drawing the headers (and/or footers), the box or line surrounding them, and the gap separating them from the body, and instead use this extra space for the body of the document.
NOTE: In the current version of J2PrinterWorks, there is nothing to prevent using too long Strings or JLabels in your headers or footers and as a consequence having them overlap or extend beyond the specified margins or outside the boxes and lines.
»
|