Java Reference
In-Depth Information
How It Works
In releases of JavaFX prior to JavaFX 8, there was no standard API for printing por-
tions of an application stage. In JavaFX 8, a Print API has been added to standardize
the way in which printing features are handled. The API also makes it easy to enable
applications with printing functionality using very little code. The API is quite large, as
it contains a number of classes, but it is very straight-forward and easy to use.
To enable print functionality for a specified node, start by working with the
javafx.print.PrinterJob class, as it contains all of the functionality for gen-
erating a very simple printing task. To send a node to the default system printer, simply
invoke PrintJob.createPrinterJob() to return a PrinterJob object.
Once the object has been returned, check to ensure that it is not null, and then call its
printPage() method, passing the node to be printed. The excerpt of the solution
that contains this functionality is shown in the following lines of code:
public void print(final Node node, Printer printer) {
PrinterJob job = PrinterJob.createPrinterJob()
job.setPrinter(printer);
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
}
}
While use of the PrinterJob is all that is required to send a node to the printer,
the API allows for much more customization. Table 14-3 lists the different classes
available in the API, along with a brief description of what they do.
Table 14-3 . JavaFX Print API
Class Name
Description
Encapsulates settings for a
print job
JobSettings
Encapsulates layout set-
tings
PageLayout
 
 
Search WWH ::




Custom Search