APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing

pack-priv Class TablePrintable

extends Object
implements Printable
Class Inheritance
All Implemented Interfaces
java.awt.print.Printable
Imports
javax.swing.table.*, java.awt.*, java.awt.print.*, java.awt.geom.*, java.text.MessageFormat

An implementation of Printable for printing JTables.

This implementation spreads table rows naturally in sequence across multiple pages, fitting as many rows as possible per page. The distribution of columns, on the other hand, is controlled by a printing mode parameter passed to the constructor. When JTable.PrintMode.NORMAL is used, the implementation handles columns in a similar manner to how it handles rows, spreading them across multiple pages (in an order consistent with the table's ComponentOrientation). When JTable.PrintMode.FIT_WIDTH is given, the implementation scales the output smaller if necessary, to ensure that all columns fit on the page. (Note that width and height are scaled equally, ensuring that the aspect ratio remains the same).

The portion of table printed on each page is headed by the appropriate section of the table's JTableHeader.

Header and footer text can be added to the output by providing MessageFormat instances to the constructor. The printing code requests Strings from the formats by calling their format method with a single parameter: an Object array containing a single element of type Integer, representing the current page number.

There are certain circumstances where this Printable cannot fit items appropriately, resulting in clipped output. These are:

It is entirely valid for a developer to wrap this Printable inside another in order to create complex reports and documents. They may even request that different pages be rendered into different sized printable areas. The implementation was designed to handle this by performing most of its calculations on the fly. However, providing different sizes works best when JTable.PrintMode.FIT_WIDTH is used, or when only the printable width is changed between pages. This is because when it is printing a set of rows in JTable.PrintMode.NORMAL and the implementation determines a need to distribute columns across pages, it assumes that all of those rows will fit on each subsequent page needed to fit the columns.

It is the responsibility of the developer to ensure that the table is not modified in any way after this Printable is created (invalid modifications include changes in: size, renderers, or underlying data). The behavior of this Printable is undefined if the table is changed at any time after creation.

Author
Shannon Hickey

Field Summary

Modifier and TypeField and Description
private final Rectangle
clip

Used to store an area of the table to be printed.

private int
col

The next column to print.

private TableColumnModel
colModel

For quick reference to the table's column model.

private Font
footerFont

The font to use in rendering footer text.

private MessageFormat
footerFormat

Provides the footer text for the table.

private static final int
H_F_SPACE

Vertical space to leave between table and header/footer text.

private final Rectangle
hclip

Used to store an area of the table's header to be printed.

private JTableHeader
header

For quick reference to the table's header.

private static final float
HEADER_FONT_SIZE

Font size for the header text.

private Font
headerFont

The font to use in rendering header text.

private MessageFormat
headerFormat

Provides the header text for the table.

private int
last

The most recent page index asked to print.

private JTable.PrintMode
printMode

The printing mode of this printable.

private int
row

The next row to print.

private JTable
table

The table to print.

private final Rectangle
tempRect

Saves the creation of multiple rectangles.

private int
totalColWidth

To save multiple calculations of total column width.

Constructor Summary

AccessConstructor and Description
public
TablePrintable(JTable
the table to print
table
,
JTable.PrintMode
the printing mode for this printable
printMode
,
MessageFormat
a MessageFormat specifying the text to be used in printing a header, or null for none
headerFormat
,
MessageFormat
a MessageFormat specifying the text to be used in printing a footer, or null for none
footerFormat
)

Create a new TablePrintable for the given JTable.

Method Summary

Modifier and TypeMethod and Description
private void
findNextClip(int
the width of the area to print in
pw
,
int
the height of the area to print in
ph
)

Calculate the area of the table to be printed for the next page.

private void
printText(Graphics2D
the graphics to draw into
g2d
,
String
the text to draw, non null
text
,
Rectangle2D
the bounding rectangle for this text, as calculated at the given font, non null
rect
,
Font
the font to draw the text in, non null
font
,
int
the width of the area to draw into
imgWidth
)

A helper method that encapsulates common code for rendering the header and footer text.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clipback to summary
private final Rectangle clip

Used to store an area of the table to be printed.

colback to summary
private int col

The next column to print.

colModelback to summary
private TableColumnModel colModel

For quick reference to the table's column model.

footerFontback to summary
private Font footerFont

The font to use in rendering footer text.

footerFormatback to summary
private MessageFormat footerFormat

Provides the footer text for the table.

H_F_SPACEback to summary
private static final int H_F_SPACE

Vertical space to leave between table and header/footer text.

hclipback to summary
private final Rectangle hclip

Used to store an area of the table's header to be printed.

HEADER_FONT_SIZEback to summary
private static final float HEADER_FONT_SIZE

Font size for the header text.

headerFontback to summary
private Font headerFont

The font to use in rendering header text.

headerFormatback to summary
private MessageFormat headerFormat

Provides the header text for the table.

lastback to summary
private int last

The most recent page index asked to print.

printModeback to summary
private JTable.PrintMode printMode

The printing mode of this printable.

rowback to summary
private int row

The next row to print.

tableback to summary
private JTable table

The table to print.

tempRectback to summary
private final Rectangle tempRect

Saves the creation of multiple rectangles.

totalColWidthback to summary
private int totalColWidth

To save multiple calculations of total column width.

Constructor Detail

TablePrintableback to summary
public TablePrintable(JTable table, JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat)

Create a new TablePrintable for the given JTable. Header and footer text can be specified using the two MessageFormat parameters. When called upon to provide a String, each format is given the current page number.

Parameters
table:JTable

the table to print

printMode:JTable.PrintMode

the printing mode for this printable

headerFormat:MessageFormat

a MessageFormat specifying the text to be used in printing a header, or null for none

footerFormat:MessageFormat

a MessageFormat specifying the text to be used in printing a footer, or null for none

Exceptions
IllegalArgumentException:
if passed an invalid print mode

Method Detail

findNextClipback to summary
private void findNextClip(int pw, int ph)

Calculate the area of the table to be printed for the next page. This should only be called if there are rows and columns left to print. To avoid an infinite loop in printing, this will always put at least one cell on each page.

Parameters
pw:int

the width of the area to print in

ph:int

the height of the area to print in

printTextback to summary
private void printText(Graphics2D g2d, String text, Rectangle2D rect, Font font, int imgWidth)

A helper method that encapsulates common code for rendering the header and footer text.

Parameters
g2d:Graphics2D

the graphics to draw into

text:String

the text to draw, non null

rect:Rectangle2D

the bounding rectangle for this text, as calculated at the given font, non null

font:Font

the font to draw the text in, non null

imgWidth:int

the width of the area to draw into