Class PrinterJob
public final class PrinterJob extends Object
It includes
- Printer discovery
- Job creation
- Job configuration based on supported printer capabilities
- Page setup
- Rendering of a node hierarchy to a page.
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.
- Since:
- JavaFX 8.0
Nested Class Summary
| Modifier and Type | Class | Description |
|---|---|---|
static enum |
PrinterJob.JobStatus |
An enum class used in reporting status of a print job. |
Property Summary
| Type | Property | Description |
|---|---|---|
final ReadOnlyObjectProperty |
jobStatus |
A read only object property representing the current JobStatus
|
final ObjectProperty |
printer |
Property representing the Printer for this job. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
void |
cancelJob() |
Cancel the underlying print job at the earliest opportunity. |
static final PrinterJob |
createPrinterJob() |
Factory method to create a job. |
static final PrinterJob |
createPrinterJob |
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. |
final PrinterJob.JobStatus |
getJobStatus() |
Gets the value of the jobStatus property. |
final Printer |
getPrinter() |
Gets the value of the printer property. |
final ReadOnlyObjectProperty |
jobStatusProperty() |
A read only object property representing the current JobStatus
|
final ObjectProperty |
printerProperty() |
Property representing the Printer for this job. |
boolean |
printPage |
Print the specified node using the specified page layout. |
boolean |
printPage |
Print the specified node. |
final void |
setPrinter |
Sets the value of the printer property. |
boolean |
showPageSetupDialog |
Displays a Page Setup dialog. |
boolean |
showPrintDialog |
Displays a Print Dialog. |
Property Details
printer
public final ObjectProperty<Printer> printerProperty
Printer for this job. When setting a printer which does not support the current job settings, (for example if DUPLEX printing is requested but the new printer does not support this), then the values are reset to the default for the new printer, or in some cases a similar value. For example this might mean REVERSE_LANDSCAPE is updated to LANDSCAPE, however this implementation optimisation is allowed, but not required. 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.
- See Also:
jobStatus
public final ReadOnlyObjectProperty<PrinterJob.JobStatus> jobStatusProperty
JobStatus
- See Also:
Method Details
createPrinterJob
public static final PrinterJob createPrinterJob()
- Returns:
- a new PrinterJob instance, or null.
createPrinterJob
public static final PrinterJob createPrinterJob(Printer printer)
The printer argument determines the initial printer
- Parameters:
-
printer- to use for the job. If the printer is currently unavailable (eg offline) then this may return null. - Returns:
- a new PrinterJob, or null.
printerProperty
public final ObjectProperty<Printer> printerProperty()
Printer for this job. When setting a printer which does not support the current job settings, (for example if DUPLEX printing is requested but the new printer does not support this), then the values are reset to the default for the new printer, or in some cases a similar value. For example this might mean REVERSE_LANDSCAPE is updated to LANDSCAPE, however this implementation optimisation is allowed, but not required. 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.
- Returns:
- the
Printerfor this job - See Also:
getPrinter
public final Printer getPrinter()
printer property.- Property description:
- Property representing the
Printerfor this job. When setting a printer which does not support the current job settings, (for example if DUPLEX printing is requested but the new printer does not support this), then the values are reset to the default for the new printer, or in some cases a similar value. For example this might mean REVERSE_LANDSCAPE is updated to LANDSCAPE, however this implementation optimisation is allowed, but not required.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.
- Returns:
- the value of the
printerproperty - See Also:
setPrinter
public final void setPrinter(Printer printer)
printer property.- Property description:
- Property representing the
Printerfor this job. When setting a printer which does not support the current job settings, (for example if DUPLEX printing is requested but the new printer does not support this), then the values are reset to the default for the new printer, or in some cases a similar value. For example this might mean REVERSE_LANDSCAPE is updated to LANDSCAPE, however this implementation optimisation is allowed, but not required.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.
- Parameters:
-
printer- the value for theprinterproperty - See Also:
getJobSettings
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.- Returns:
- current job settings.
showPrintDialog
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.
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 Platform.runLater. It must not be called during animation or layout processing.
- Parameters:
-
owner- to which to block input, or null. - 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.
- Throws:
-
IllegalStateException- if this method is called during animation or layout processing.
showPageSetupDialog
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.
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 Platform.runLater. It must not be called during animation or layout processing.
- Parameters:
-
owner- to block input, or null. - 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.
- Throws:
-
IllegalStateException- if this method is called during animation or layout processing.
printPage
public boolean printPage(PageLayout pageLayout, Node node)
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 Platform.runLater. It must not be called during animation or layout processing.
- Parameters:
-
pageLayout- Layout for this page. -
node- The node to print. - Returns:
- whether rendering was successful.
- Throws:
-
NullPointerException- if either parameter is null. -
IllegalStateException- if this method is called during animation or layout processing.
printPage
public boolean printPage(Node node)
- Parameters:
-
node- The node to print. - Returns:
- whether rendering was successful.
- Throws:
-
NullPointerException- if the node parameter is null.
jobStatusProperty
public final ReadOnlyObjectProperty<PrinterJob.JobStatus> jobStatusProperty()
JobStatus
- Returns:
- the current
JobStatus - See Also:
getJobStatus
public final PrinterJob.JobStatus getJobStatus()
jobStatus property.- Property description:
- A read only object property representing the current
JobStatus - Returns:
- the value of the
jobStatusproperty - See Also:
cancelJob
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.
endJob
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()}.
- Returns:
- true if job is spooled, false if its not, or the job was already in a completed state.
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.