public final class PrinterJob extends Object
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.
Type | Property and Description |
---|---|
ReadOnlyObjectProperty<PrinterJob.JobStatus> |
jobStatus
A read only object property representing the current
JobStatus |
ObjectProperty<Printer> |
printer
Property representing the
Printer for this job. |
Modifier and Type | Class and Description |
---|---|
static class |
PrinterJob.JobStatus
An enum class used in reporting status of a print job.
|
Modifier and Type | Method and Description |
---|---|
void |
cancelJob()
Cancel the underlying print job at the earliest opportunity.
|
static PrinterJob |
createPrinterJob()
Factory method to create a job.
|
static PrinterJob |
createPrinterJob(Printer printer)
Factory method to create a job for a specified printer.
|
boolean |
endJob()
If the job can be successfully spooled to the printer queue
this will return true.
|
JobSettings |
getJobSettings()
The
JobSettings encapsulates all the API supported job
configuration options such as number of copies,
collation option, duplex option, etc. |
PrinterJob.JobStatus |
getJobStatus()
Obtain the current status of the job.
|
Printer |
getPrinter()
Gets the printer currently associated with this job.
|
ReadOnlyObjectProperty<PrinterJob.JobStatus> |
jobStatusProperty()
A read only object property representing the current
JobStatus |
ObjectProperty<Printer> |
printerProperty()
Property representing the
Printer for this job. |
boolean |
printPage(Node node)
Print the specified node.
|
boolean |
printPage(PageLayout pageLayout,
Node node)
Print the specified node using the specified page layout.
|
void |
setPrinter(Printer printer)
Change the printer for this job.
|
boolean |
showPageSetupDialog(Window owner)
Displays a Page Setup dialog.
|
boolean |
showPrintDialog(Window owner)
Displays a Print Dialog.
|
String |
toString() |
public final ObjectProperty<Printer> printerProperty
Printer
for this job.getPrinter()
,
setPrinter(Printer)
public ReadOnlyObjectProperty<PrinterJob.JobStatus> jobStatusProperty
JobStatus
getJobStatus()
public static final PrinterJob createPrinterJob()
SecurityException
- if a job does not have permission
to initiate a printer job.public static final PrinterJob createPrinterJob(Printer printer)
The printer
argument determines the initial printer
printer
- to use for the job. If the printer is currently
unavailable (eg offline) then this may return null.SecurityException
- if a job does not have permission
to initiate a printer job.public final ObjectProperty<Printer> printerProperty()
Printer
for this job.getPrinter()
,
setPrinter(Printer)
public Printer getPrinter()
public void setPrinter(Printer printer)
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.
printer
- to be used for this print job.public JobSettings getJobSettings()
JobSettings
encapsulates all the API supported job
configuration options such as number of copies,
collation option, duplex option, etc.
The initial values are based on the current settings for
the initial printer.public boolean showPrintDialog(Window owner)
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 owner
may be null, but
if it is a visible Window, it will be used as the parent.
owner
- to which to block input, or null.public boolean showPageSetupDialog(Window owner)
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 owner
may be null, but
if it is a visible Window, it will be used as the parent.
owner
- to block input, or null.public boolean printPage(PageLayout pageLayout, Node node)
pageLayout
- Layout for this page.node
- The node to print.NullPointerException
- if either parameter is null.public boolean printPage(Node node)
node
- The node to print.NullPointerException
- if the node parameter is null.public ReadOnlyObjectProperty<PrinterJob.JobStatus> jobStatusProperty()
JobStatus
getJobStatus()
public PrinterJob.JobStatus getJobStatus()
JobStatus
public void cancelJob()
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.
public boolean endJob()
A return value of false means the job could not be spooled, or was already completed.
Successful completion will also update job status to DONE
,
at which point the job can no longer be used.
Calling endJob() on a job for which no pages have been printed is equivalent to calling {code cancelJob()}.
Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.