Prints the content of this
JTextComponent
. Note: this method blocks until printing is done.
Page header and footer text can be added to the output by providing MessageFormat
arguments. The printing code requests Strings
from the formats, providing a single item which may be included in the formatted string: an Integer
representing the current page number.
showPrintDialog boolean
parameter allows you to specify whether a print dialog is displayed to the user. When it is, the user may use the dialog to change printing attributes or even cancel the print.
service
allows you to provide the initial PrintService
for the print dialog, or to specify PrintService
to print to when the dialog is not shown.
attributes
can be used to provide the initial values for the print dialog, or to supply any needed attributes when the dialog is not shown. attributes
can be used to control how the job will print, for example duplex or single-sided.
interactive boolean
parameter allows you to specify whether to perform printing in interactive mode. If true
, a progress dialog, with an abort option, is displayed for the duration of printing. This dialog is modal when print
is invoked on the Event Dispatch Thread and non-modal otherwise. Warning: calling this method on the Event Dispatch Thread with interactive false
blocks all events, including repaints, from being processed until printing is complete. It is only recommended when printing from an application with no visible GUI.
Note: In headless mode, showPrintDialog
and interactive
parameters are ignored and no dialogs are shown.
This method ensures the document
is not mutated during printing. To indicate it visually, setEnabled(false)
is set for the duration of printing.
This method uses getPrintable(java.text.MessageFormat, java.text.MessageFormat)
to render document content.
This method is thread-safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
Sample Usage . This code snippet shows a cross-platform print dialog and then prints the JTextComponent
in interactive mode unless the user cancels the dialog:
textComponent.print(new MessageFormat("My text component header"),
new MessageFormat("Footer. Page - {0}"), true, null, null, true);
Executing this code off the Event Dispatch Thread performs printing on the background. The following pattern might be used for background printing:
FutureTask<Boolean> future =
new FutureTask<Boolean>(
new Callable<Boolean>() {
public Boolean call() {
return textComponent.print(.....);
}
});
executor.execute(future);