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:
ComponentOrientation.
JTable.PrintMode.NORMAL when a column
is too wide to fit in the printable area. The center of the
column is printed and no left and right borders are shown.
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.
| Modifier and Type | Field 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 static final float | FOOTER_FONT_SIZE
Font size for the footer text. |
| 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
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. |
| Access | Constructor and Description |
|---|---|
| public | TablePrintable(JTable
the table to print table, JTable.the printing mode for this printable printMode,a headerFormat, MessageFormat MessageFormat specifying the text to
be used in printing a header, or null for nonea footerFormat)MessageFormat specifying the text to
be used in printing a footer, or null for noneCreate a new |
| Modifier and Type | Method 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. |
| public int | Returns: PAGE_EXISTS if the page is rendered successfully, or NO_SUCH_PAGE if a non-existent page index is specifiedthe context into which the page is drawn graphics, PageFormat the size and orientation of the page being drawn pageFormat, int the zero based index of the page to be drawn pageIndex)Implements java. Prints the specified page of the table into the given |
| 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. |
| clip | back to summary |
|---|---|
| private final Rectangle clip Used to store an area of the table to be printed. | |
| col | back to summary |
|---|---|
| private int col The next column to print. | |
| colModel | back to summary |
|---|---|
| private TableColumnModel colModel For quick reference to the table's column model. | |
| FOOTER_FONT_SIZE | back to summary |
|---|---|
| private static final float FOOTER_FONT_SIZE Font size for the footer text. | |
| footerFont | back to summary |
|---|---|
| private Font footerFont The font to use in rendering footer text. | |
| footerFormat | back to summary |
|---|---|
| private MessageFormat footerFormat Provides the footer text for the table. | |
| H_F_SPACE | back to summary |
|---|---|
| private static final int H_F_SPACE Vertical space to leave between table and header/footer text. | |
| hclip | back to summary |
|---|---|
| private final Rectangle hclip Used to store an area of the table's header to be printed. | |
| header | back to summary |
|---|---|
| private JTableHeader header For quick reference to the table's header. | |
| HEADER_FONT_SIZE | back to summary |
|---|---|
| private static final float HEADER_FONT_SIZE Font size for the header text. | |
| headerFont | back to summary |
|---|---|
| private Font headerFont The font to use in rendering header text. | |
| headerFormat | back to summary |
|---|---|
| private MessageFormat headerFormat Provides the header text for the table. | |
| last | back to summary |
|---|---|
| private int last The most recent page index asked to print. | |
| printMode | back to summary |
|---|---|
| private JTable. The printing mode of this printable. | |
| row | back to summary |
|---|---|
| private int row The next row to print. | |
| table | back to summary |
|---|---|
| private JTable table The table to print. | |
| tempRect | back to summary |
|---|---|
| private final Rectangle tempRect Saves the creation of multiple rectangles. | |
| totalColWidth | back to summary |
|---|---|
| private int totalColWidth To save multiple calculations of total column width. | |
| TablePrintable | back to summary |
|---|---|
| public TablePrintable(JTable table, JTable. Create a new
| |
| findNextClip | back 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.
| |
| back to summary | |
|---|---|
| public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException Implements java. Prints the specified page of the table into the given
| |
| printText | back 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.
| |