How to Print Tables

The JTable class provides support for printing tables. The JTable printing API includes methods that allow you to implement both basic and advanced printing tasks. For common printing tasks, when you need to simply print a table, use the print method directly. The print method has several forms with various argument sets. This method prepares your table, 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 table is to call the print method without parameters. See the code example below.

try {
    boolean complete = table.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 table is printed interactively in the FIT_WIDTH mode without a header or a footer. The code example below shows the print method signature with the complete set of arguments.

boolean complete = table.print(JTable.PrintMode printMode,
                               MessageFormat headerFormat,
                               MessageFormat footerFormat, 
                               boolean showPrintDialog,
                               PrintRequestAttributeSet attr,
                               boolean interactive,
                               PrintService service);

When you call the print method with all arguments, you explicitly choose printing features such as a printing mode, a header and a 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 JTable printing API provides the following features:

Printing Interactively or Non-interactively

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.

A screenshot of a printing progress dialog

This dialog enables the user to keep track of printing progress. The progress dialog is modal, which means that while it is shown on the screen, the user cannot interact with the table. It is important that your table remain unchanged while it is being printed, otherwise the printing behavior will be undefined. Nevertheless, printing interactively does not block other developer's code from changing the table. For example, there is another thread that posts updates using the SwingUtilities.invokeLater method. Therefore, to ensure correct printing behavior, you should be sure that your own code refrains from modifying the table during printing.

Alternatively, you can print your table non-interactively. In this mode, printing begins immediately on the event dispatch thread and completely blocks any events to be processed. On the one hand, this mode securely keeps the table against any changes until printing is done. On the other hand, this mode completely deprives the user of any interaction with the GUI. That is why printing non-interactively can only be recommended when printing from applications with non-visible GUI.

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

A screenshot of a printing dialog

You may notice that the print dialog does not specify the total number of pages in the printout. This is because the table printing implementation uses the Printable API and the total number of pages is not known ahead of 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 some 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".

Printing Modes

Printing modes are responsible for scaling the output and spreading it across pages. You can print your table in one of the following modes:

  • PrintMode.NORMAL

  • PrintMode.FIT_WIDTH

In the NORMAL mode a table is printed at its current size. If columns do not fit a page, they spread across additional pages according to the table's ComponentOrientation . In the FIT_WIDTH mode a table has a smaller size, if necessary, to fit all columns on each page. Note that both width and height are scaled to provide an output of the same aspect ratio. In both modes rows spread across multiple pages sequentially with as many rows on a page as possible.

Automatic Layout and Pagination

With the use of the JTable printing API you do not need to take care of layout and pagination. You only need to specify appropriate parameters to the print method such as printing mode and footer text format (if you want to insert the page number in the footer). As demonstrated earlier, you can specify the page number in your footer by including "{0}" in the string given to the MessageFormat footer parameter. In the printed output, {0} will be replaced by the current page number.

Table Printing Examples

Let us look at an example called TablePrintDemo1 . The entire code for this program can be found in This demo's rich GUI is built automatically by the NetBeans IDE GUI builder. Here is a picture of the TablePrintDemo1 application.

A screenshot of TablePrintDemo1


boolean print()
boolean print(printMode)
boolean print(printMode, MessageFormat, MessageFormat)
boolean print(printMode, MessageFormat, MessageFormat, boolean, PrintRequestAttributeSet, boolean)
boolean print(printMode, MessageFormat, MessageFormat, boolean, PrintRequestAttributeSet, boolean, PrintService) When called without arguments, displays a print dialog, and then prints this table interactively in the FIT_WIDTH mode without a header or a footer text. Returns true if the user continued printing and false if the user cancelled printing.
When called with a full set of arguments, prints this table according to the specified arguments. The first argument specifies the printing mode. 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.

« PreviousTrailNext »