The JTextComponent
class provides support for printing text documents. The JTextComponent
API includes methods that allow you to implement both basic and advanced printing tasks. Supported formats include HTML, RTF, and plain text. For common printing tasks such as simply printing a text document, use the print
method directly. The print
method has several forms with various argument sets. This method prepares your text document, gets a corresponding Printable
object, and sends it to a printer.
If the default implementation of the Printable
object does not meet your needs, you can customize the printing layout by overriding the getPrintable
method to wrap the default Printable
or even replace it altogether.
The easiest way to print your text component is to call the print
method without parameters. See the code example below.
try { boolean complete = textComponent.print(); if (complete) { /* show a success message */ ... } else { /*show a message indicating that printing was cancelled */ ... } } catch (PrinterException pe) { /* Printing failed, report to the user */ ... }
When you call the print
method with no parameters, a print dialog is displayed, and then your text component is printed interactively without a header or a footer. The code example below shows the print
method signature with the complete set of arguments.
boolean complete = textComponent.print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service PrintRequestAttributeSet attributes, boolean interactive);
When you call the print
method with all arguments, you explicitly choose printing features such as header and footer text, printing attributes, a destination print service, and also whether to show a print dialog or not, and whether to print interactively or non-interactively. To decide which parameters suit your needs best, see the description of available features below.
The JTextComponent
printing API provides the following features:
In interactive mode a progress dialog with an abort option is shown for the duration of printing. Here is a sample of a progress dialog.
This dialog allows the user to keep track of printing progress. The progress dialog is modal when the print
method is called on the event dispatch thread and non-modal otherwise. It is important that your document remain unchanged while being printed, otherwise the printing behavior is undefined. The print
method ensures that your document will not be changed and disables the component for the duration of printing.
If you call the print
method on the event dispatch thread in non-interactive mode, then all events including repaints will be blocked. That is why printing non-interactively on EDT is only recommended for applications with non-visible GUI.
You can display a standard print dialog which allows the user to do the following:
You may notice that the print dialog does not specify the total number of pages in the printout. This is because the text printing implementation uses the Printable
API and the total number of pages is not known before printing time.
Headers and footers are provided by
MessageFormat
parameters. These parameters allow the header and footer to be localized. Read the documentation for the
MessageFormat
class as characters such as single quotes are special and need to be avoided. Both headers and footers are centered. You can insert a page number by using {0}
.
MessageFormat footer = new MessageFormat("Page - {0}");
Since the total number of pages in the output is not known before printing time, there is no way to specify a numbering format like "Page 1 of 5".
Let us look at an example called TextAreaPrintingDemo
. The main feature of this demo is printing a text document either on the event dispatch thread or on a background thread depending on the user's choice. This demo displays a text area, allows to select several printing features, and prints the text area's content according to the selected options. The entire code for this program can be found in
TextAreaPrintingDemo.java
. This demo's rich GUI is built in the
NetBeans IDE GUI builder. Here is a picture of the TextAreaPrintingDemo
application.
Whenever a web-launched application tries to print, Java Web Start opens up a security dialog asking the user for permission to print unless this permission has already been granted in the system settings. To proceed with printing the user has to accept the request.
An action listener is registered for the Print button. As the user clicks the Print button the actionPerformed
method calls the print
method, which initiates a printing task. The printing task is a SwingWorker
object. The code example below shows how the PrintingTask
class is implemented.
private class PrintingTask extends SwingWorker
The code example below shows how the print
method obtains the set of selected options from the GUI components, then creates an instance of the PrintingTask
class, and performs printing.
private void print(java.awt.event.ActionEvent evt) { MessageFormat header = createFormat(headerField); MessageFormat footer = createFormat(footerField); boolean interactive = interactiveCheck.isSelected(); boolean background = backgroundCheck.isSelected(); PrintingTask task = new PrintingTask(header, footer, interactive); if (background) { task.execute(); } else { task.run() } }
The code in bold illustrates how PrintingTask
's methods are invoked depending on the background
parameter's value. Whenever the user prefers to print on a background thread, the execute
method is called, which schedules the printing task for the execution on a background thread. Otherwise the run
method performs the printing task on EDT.
Since printing large documents is a time-consuming task, it is recommended to perform printing on a background thread.
The TextBatchPrintingDemo
example illustrates printing non-visible HTML text documents on background threads. When launched, this demo displays a page with a list of URLs. You can visit an HTML page, add the displayed page to the print list, and once you select all pages that you need, you can print them all at once on background threads. The entire code for this program can be found in
TextBatchPrintingDemo.java
. Here is a picture of the TextBatchPrintingDemo
application.
You can find the printing code in the printSelectedPages
method. When called, this method first obtains the amount of pages selected for printing. The code example below shows how the printSelectedPages
method creates a Runnable
object for each page and then prints the current page on a separate thread.
for (int i = 0; i < n; i++) { final PageItem item = (PageItem) pages.getElementAt(i); // This method is called from EDT. Printing is a time-consuming // task, so it should be done outside EDT, in a separate thread. Runnable printTask = new Runnable() { public void run() { try { item.print( // Two "false" args mean "no print dialog" and // "non-interactive" (ie, batch-mode printing). null, null, false, printService, null, false); } catch (PrinterException pe) { JOptionPane.showMessageDialog(null, "Error printing " + item.getPage() + "\n" + pe, "Print Error", JOptionPane.WARNING_MESSAGE); } } }; new Thread(printTask).start();
This section lists methods defined in the JTextComponent
class that allow you to print text documents.
Method | Purpose |
---|---|
boolean print() boolean print(MessageFormat, MessageFormat) boolean print(MessageFormat, MessageFormat, boolean, PrintRequestAttributeSet, boolean, PrintService) |
When called without arguments, displays a print dialog, and then prints this text component interactively without a header or a footer text. Returns true if the user continued printing and false if the user cancelled printing.When called with the two MessageFormat arguments, displays a print dialog, and then prints this text component interactively with the specified header and footer text.When called with a full set of arguments, prints this text component according to the specified arguments. The two MessageFormat arguments specify header and footer text. The first boolean argument defines whether to show a print dialog or not. Another boolean argument specifies whether to print interactively or not. With two other arguments you can specify printing attributes and a print service.Whenever a PrintService argument is omitted, the default printer will be used. |
Printable getPrintable(MessageFormat, MessageFormat) | Returns a Printable object for printing your text component. Override this method to get a customized Printable object. You can wrap one Printable object into another in order to obtain complex reports and documents. |
This table lists examples that use text printing and points to where those examples are described.
Example | Where Described | Notes |
---|---|---|
TextAreaPrintingDemo |
This page | Demonstrates the basics of text printing and provides a rich GUI. Allows the user to specify header or footer text, turn the print dialog on or off, select printing interactively or non-interactively, and then print according to the selected options. |
TextBatchPrintingDemo |
This page | This demo displays a text component with a list of URLs, allows the user to view HTML pages, add them to the print list, and print all selected pages at once on background threads. |