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 JTextComponent.getPrintable(java.text.MessageFormat, java.text.MessageFormat)
to render document content.
This method is thread-safe, although most Swing methods are not. Please
see
How to Use Threads 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);
Returns:
{@code true}, unless printing is canceled by the user
Parameters:
-
headerFormat - the text, in {@code MessageFormat}, to be
used as the header, or {@code null} for no header
-
footerFormat - the text, in {@code MessageFormat}, to be
used as the footer, or {@code null} for no footer
-
showPrintDialog - {@code true} to display a print dialog,
{@code false} otherwise
-
service - initial {@code PrintService}, or {@code null} for the
default
-
attributes - the job attributes to be applied to the print job, or
{@code null} for none
-
interactive - whether to print in an interactive mode
Throws:
-
PrinterException - if an error in the print system causes the job
to be aborted
-
SecurityException - if this thread is not allowed to
initiate a print job request
Since:
1.6
See Also:
JTextComponent.getPrintable(java.text.MessageFormat, java.text.MessageFormat)
,
MessageFormat
,
GraphicsEnvironment.isHeadless()
,
FutureTask
,