TheJTextComponent
class provides support for printing text documents. TheJTextComponent
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 thePrintable
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 thegetPrintable
method to wrap the defaultPrintable
or even replace it altogether.The easiest way to print your text component is to call the
When you call thetry { 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 theboolean complete = textComponent.print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service PrintRequestAttributeSet attributes, boolean interactive);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 If you call the
You can display a standard print dialog which allows the user to do the following:
- Select a printer
- Specify number of copies
- Change printing attributes
- Cancel printing before it has been started
- Start printing
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 byMessageFormat
parameters. These parameters allow the header and footer to be localized. Read the documentation for theMessageFormat
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".
With the use of theJTextComponent
printing API you do not need to take care of layout and pagination. Both layout and pagination are done automatically. The document content is formatted to fit the page size and spreads across multiple pages. You only need to specify an appropriate footer text format to the"{0}"
in the string given to theMessageFormat
footer parameter. In the printed output, {0} will be replaced by the current page number.
Let us look at an example calledTextAreaPrintingDemo
. 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 inTextAreaPrintingDemo.java
. This demo's rich GUI is built in the NetBeans IDE GUI builder. Here is a picture of theTextAreaPrintingDemo
application.
Try this: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.
- Click the Launch button to run TextAreaPrintingDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.
- Edit the text in the Header or Footer checkboxes or both to provide a different header or footer.
- Clear the Show Progress Dialog checkbox if you want to print without displaying a progress dialog, which means printing non-interactively. Note that you will not be able to cancel printing once it has been started.
- Clear the Print in Background checkbox to select printing on the event dispatch thread. Note that printing on EDT non-interactively will make your application unresponsive — interaction with your application will be blocked for the duration of the printing process.
- Click the Print button to print the text area's content according to the selected options.
An action listener is registered for the Print button. As the user clicks the Print button the
actionPerformed
method calls theSwingWorker
object. The code example below shows how thePrintingTask
class is implemented.The code example below shows how theprivate class PrintingTask extends SwingWorkerPrintingTask
class, and performs printing.The code in bold illustrates howprivate 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() } }PrintingTask
's methods are invoked depending on thebackground
parameter's value. Whenever the user prefers to print on a background thread, theexecute
method is called, which schedules the printing task for the execution on a background thread. Otherwise therun
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.
Text Batch Printing Example
TheTextBatchPrintingDemo
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 inTextBatchPrintingDemo.java
. Here is a picture of theTextBatchPrintingDemo
application.
Try this:You can find the printing code in the
- Click the Launch button to run TextBatchPrintingDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.
- Click on any link to view the corresponding HTML page.
- Press ALT+A or choose File > Add Page menu item to add the displayed page to a print list shown on the right.
- Press ALT+H or choose File > Home Page menu item to return to the demo's home page.
- Add as many pages to the print list as you need.
- Press ALT+C or choose File > Clear Selected menu item if you need to clear the print list and build at again.
- Press ALT+P or choose File > Print Selected menu item to print the selected pages.
- Press ALT+Q or choose File > Quit menu item to quit the application.
printSelectedPages
method. When called, this method first obtains the amount of pages selected for printing. The code example below shows how theprintSelectedPages
method creates aRunnable
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();Text Printing API
This section lists methods defined in theJTextComponent
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 andfalse
if the user cancelled printing.
When called with the twoMessageFormat
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 twoMessageFormat
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 aPrintService
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.Examples That Use Text Printing
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.