It includes
Here ia a very simple example, which prints a single node.
Node node = new Circle(100, 200, 200);
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
}
Points to note
In the example above the node was not added to a scene. Since most printing scenarios are printing content that's either not displayed at all, or must be prepared and formatted differently, this is perfectly acceptable.
If content that is currently part of a Scene and is being displayed, is printed, then because printing a job or even a single page of the job may span over multiple screen "pulses" or frames, it is important for the application to ensure that the node being printed is not updated during the printing process, else partial or smeared rendering is probable.
It should be apparent that the same applies even to nodes that are not displayed - updating them concurrent with printing them is not a good idea.
There is no requirement to do printing on the FX application thread. A node may be prepared for printing on any thread, the job may be invoked on any thread. However, minimising the amount of work done on the FX application thread is generally desirable, so as not to affect the responsiveness of the application UI. So the recommendation is to perform printing on a new thread and let the implementation internally schedule any tasks that need to be performed on the FX thread to be run on that thread.
| Modifier and Type | Class and Description |
|---|---|
| public static enum | PrinterJob.
An enum class used in reporting status of a print job. |
| Modifier and Type | Field and Description |
|---|---|
| private PrinterJobImpl | |
| private ReadOnlyObjectWrapper | |
| private ObjectProperty | |
| private JobSettings |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| private synchronized PrinterJobImpl | |
| public static final PrinterJob | |
| public static final PrinterJob | Returns: a new PrinterJob, or null.to use for the job. If the printer is currently
unavailable (eg offline) then this may return null. printer)Factory method to create a job for a specified printer. |
| private ObjectProperty | |
| public synchronized boolean | Returns: true if job is spooled, false if its not, or the job was already in a completed state.If the job can be successfully spooled to the printer queue this will return true. |
| public synchronized JobSettings | Returns: current job settings.The |
| public final PrinterJob. | |
| public final synchronized Printer | |
| pack-priv boolean | isJobNew()
Updating settings or printer is only allowed on a new job, meaning before you start printing or cancel etc. The implementation needs to check this wherever job state updates are received. |
| public final ReadOnlyObjectProperty | Returns: the currentJobStatusA read only object property representing the current
|
| public final ObjectProperty | |
| public synchronized boolean | Returns: whether rendering was successful.Layout for this page. pageLayout, Node The node to print. node)Print the specified node using the specified page layout. |
| public synchronized boolean | |
| public final synchronized void | |
| public synchronized boolean | Returns: false if the user opts to cancel the dialog, or the job is not in the new state. That is if it has already started, has failed, or has been cancelled, or ended.to block input, or null. owner)Displays a Page Setup dialog. |
| public synchronized boolean | Returns: false if the user opts to cancel printing, or the job is not in the new state. That is if it has already started, has failed, or has been cancelled, or ended.to which to block input, or null. owner)Displays a Print Dialog. |
| public String | |
| pack-priv synchronized PageLayout | Returns: aPageLayout that is supported in the
current job configuration.to be validated pageLayout)This method can be used to check if a page configuration is possible in the current job configuration. |
| jobImpl | back to summary |
|---|---|
| private PrinterJobImpl jobImpl | |
| jobStatus | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<PrinterJob. | |
| printer | back to summary |
|---|---|
| private ObjectProperty<Printer> printer | |
| settings | back to summary |
|---|---|
| private JobSettings settings | |
| PrinterJob | back to summary |
|---|---|
| private PrinterJob(Printer printer) | |
| cancelJob | back to summary |
|---|---|
| public void cancelJob() Cancel the underlying print job at the earliest opportunity. It may return immediately without waiting for the job cancellation to be complete in case this would block the FX user thread for any period of time. If printing is in process at that time, then typically this means cancellation is after the current page is rendered. The job status is updated to CANCELED only once that has happened. Thus determining that the job is CANCELED requires monitoring the job status. The call has no effect if the job has already been requested to be CANCELED, or is in the state ERROR or DONE. For example it will not de-queue from the printer a job that has already been spooled for printing. Once a job is cancelled, it is not valid to call any methods which render new content or change job state. | |
| createImplJob | back to summary |
|---|---|
| private synchronized PrinterJobImpl createImplJob(Printer printer, JobSettings settings) | |
| createPrinterJob | back to summary |
|---|---|
| public static final PrinterJob createPrinterJob() Factory method to create a job. If there are no printers available, this will return null. Some platforms may provide a pseudo printer, which creates a document. These will be enumerated here so long as the platform also enumerates them as if they are printers.
| |
| createPrinterJob | back to summary |
|---|---|
| public static final PrinterJob createPrinterJob(Printer printer) Factory method to create a job for a specified printer.
The
| |
| createPrinterProperty | back to summary |
|---|---|
| private ObjectProperty | |
| endJob | back to summary |
|---|---|
| public synchronized boolean endJob() If the job can be successfully spooled to the printer queue this will return true. Note : this does not mean the job already printed as that could entail waiting for minutes or longer, even where implementable. A return value of false means the job could not be spooled, or was already completed.
Successful completion will also update job status to Calling endJob() on a job for which no pages have been printed is equivalent to calling {code cancelJob()}.
| |
| getJobSettings | back to summary |
|---|---|
| public synchronized JobSettings getJobSettings() The
| |
| getJobStatus | back to summary |
|---|---|
| public final PrinterJob. | |
| getPrinter | back to summary |
|---|---|
| public final synchronized Printer getPrinter() | |
| isJobNew | back to summary |
|---|---|
| pack-priv boolean isJobNew() Updating settings or printer is only allowed on a new job, meaning before you start printing or cancel etc. The implementation needs to check this wherever job state updates are received. | |
| jobStatusProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty A read only object property representing the current
| |
| printerProperty | back to summary |
|---|---|
| public final ObjectProperty Property representing the The above applies whether the printer is changed by directly calling this method, or as a side-effect of user interaction with a print dialog. Setting a null value for printer will install the default printer. Setting the current printer has no effect.
| |
| printPage | back to summary |
|---|---|
| public synchronized boolean printPage(PageLayout pageLayout, Node node) Print the specified node using the specified page layout. The page layout will override the job default for this page only. If the job state is CANCELED, ERROR or DONE, this method will return false.
This method may be called from any thread. If it is called from the FX
application thread, then it must either be called from an input event
handler or from the run method of a Runnable passed to
| |
| printPage | back to summary |
|---|---|
| public synchronized boolean printPage(Node node) Print the specified node. The page layout is the job default. If the job state is CANCELED, ERROR or DONE, this method will return false.
| |
| setPrinter | back to summary |
|---|---|
| public final synchronized void setPrinter(Printer printer) | |
| showPageSetupDialog | back to summary |
|---|---|
| public synchronized boolean showPageSetupDialog(Window owner) Displays a Page Setup dialog. A page set up dialog is primarily to allow an end user to configure the layout of a page. Paper size and orientation are the most common and most important components of this. This will display the most appropriate available dialog for this purpose. However there may be still be access to other settings, including changing the current printer. Therefore a side effect of this dialog display method may be to update that and any other current job settings. The method returns true if the user confirmed the dialog whether or not any changes are made. If the job is not in a state to display the dialog, such as already printing, cancelled or done, then the dialog will not be displayed and the method will return false.
The window
This method may be called from any thread. If it is called from the FX
application thread, then it must either be called from an input event
handler or from the run method of a Runnable passed to
| |
| showPrintDialog | back to summary |
|---|---|
| public synchronized boolean showPrintDialog(Window owner) Displays a Print Dialog. Allow the user to update job state such as printer and settings. These changes will be available in the appropriate properties after the print dialog has returned. The print dialog is also typically used to confirm the user wants to proceed with printing. This is not binding on the application but generally should be obeyed. In the case that there is no UI available then this method returns true, with no options changed, as if the user had confirmed to proceed with printing. If the job is not in a state to display the dialog, such as already printing, cancelled or done, then the dialog will not be displayed and the method will return false.
The window
This method may be called from any thread. If it is called from the
JavaFX application thread, then it must either be called from an input
event handler or from the run method of a Runnable passed to
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| validatePageLayout | back to summary |
|---|---|
| pack-priv synchronized PageLayout validatePageLayout(PageLayout pageLayout) This method can be used to check if a page configuration is possible in the current job configuration. For example if the specified paper size is supported. If the original PageLayout is supported it will be returned. If not, a new PageLayout will be returned that attempts to honour the supplied PageLayout, but adjusted to match the current job configuration. This method does not update the job configuration.
| |
jobStatus property, or may query it directly
using getJobStatus().
The typical life cycle of a job is as follows :
NOT_STARTED and
will stay there during configuration via dialogs etc.
PRINTING when the first page
is printed.
DONE once the job is
successfully completed without being cancelled or encountering
an error. The job is now completed.
ERROR or is
CANCELED is also considered completed.
A job may not revert to an earlier status in its life cycle and the current job state affects operations that may be performed. For example a job may not begin printing again if it has previously passed that state and entered any of the termination states.
| Modifier and Type | Field and Description |
|---|---|
| public static final PrinterJob. | CANCELED
The job has been cancelled by the application. |
| public static final PrinterJob. | DONE
The job initiated printing and later called endJob() which reported success. |
| public static final PrinterJob. | ERROR
The job encountered an error. |
| public static final PrinterJob. | NOT_STARTED
The new job status. |
| public static final PrinterJob. | PRINTING
The job has requested to print at least one page, and has not terminated printing. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static PrinterJob. | |
| public static PrinterJob. |
| CANCELED | back to summary |
|---|---|
| public static final PrinterJob. The job has been cancelled by the application. May not display dialogs or initiate printing. Job should be discarded. There is no need to call endJob(). | |
| DONE | back to summary |
|---|---|
| public static final PrinterJob. The job initiated printing and later called endJob() which reported success. The job can be discarded as it cannot be re-used. | |
| ERROR | back to summary |
|---|---|
| public static final PrinterJob. The job encountered an error. Job should be discarded. There is no need to call endJob(). | |
| NOT_STARTED | back to summary |
|---|---|
| public static final PrinterJob. The new job status. May display print dialogs and configure the job and initiate printing. | |
| PRINTING | back to summary |
|---|---|
| public static final PrinterJob. The job has requested to print at least one page, and has not terminated printing. May no longer display print dialogs. | |
| JobStatus | back to summary |
|---|---|
| private JobStatus() | |
| valueOf | back to summary |
|---|---|
| public static PrinterJob. | |
| values | back to summary |
|---|---|
| public static PrinterJob. | |