public final class PrintManager extends Object
The Print Manager is powerful functionality to preview and
send data out to printer. Print action from File
menu (Ctrl+Alt+Shift+P shortcut) invokes the Print Preview
dialog. The Print Preview dialog provides page layout, the set of options
including font, color, header, footer, printer settings such as paper size
and orientation, number of copies, margins, collation and system properties.
If the data is a Swing component which extends JComponent
and shown in a TopComponent, the key PRINT_PRINTABLE
with value "Boolean.TRUE" in the component must be set as a
client property. See example:
public class MyComponent extends javax.swing.JComponent {
public MyComponent() {
...
putClientProperty("print.printable", Boolean.TRUE); // NOI18N
}
...
}
The key PRINT_NAME is used to specify the name of the component
which will be printed in the header/footer:
putClientProperty("print.name", <name>); // NOI18N
If the key is not set at all, the display name of the top
component is used by default. The content of the header/footer
can be adjusted in the Print Options dialog.If the size of the custom component for printing differs from visual dimension, specify this with the key PRINT_SIZE:
putClientProperty("print.size", new Dimension(printWidth, printHeight)); // NOI18N
If the custom data is presented by several components, all of them can
be enabled for print preview. The key PRINT_ORDER is used
for this purpose, all visible and printable components are ordered
and shown in the Print Preview dialog from the left to right:
putClientProperty("print.order", <order>); // NOI18N
If the custom data is presented by another classes, a PrintProvider
should be implemented and put in the lookup
of the top component where the custom data lives.
How to put the Print action on custom Swing tool bar:
public class MyComponent extends javax.swing.JComponent {
...
JToolBar toolbar = new JToolBar();
// print
toolbar.addSeparator();
toolbar.add(PrintManager.printAction(this));
...
}
How does Print action from the main menu decide what to print?
At first, the manager searches for PrintProvider in the
lookup of the active top component.
If a print provider is found, it is used by the print manager for print preview.
Otherwise, it tries to obtain printable components among the descendants of the active top component. All found printable components are passed into the Print Preview dialog. Note that print method is invoked by the manager for preview and printing the component.
If there are no printable components, printable data are retrieved from the
selected nodes of the active top component.
The Print manager gets EditorCookie or InputStream from the selected
nodes. The StyledDocument,
returned by the editor cookie, or string, returned by the stream, contain printing
information (text, font, color). This information is shown in the print preview.
So, any textual documents (Java/C++/Php/... sources, html, xml, plain text, etc.)
are printable by default.
PrintProvider| Modifier and Type | Field and Description |
|---|---|
static String |
PRINT_NAME
This key indicates the name of the component being printed.
|
static String |
PRINT_ORDER
This key indicates the order of the component being printed.
|
static String |
PRINT_PRINTABLE
This key indicates whether the component is printable.
|
static String |
PRINT_SIZE
This key indicates the size of the component being printed.
|
| Modifier and Type | Method and Description |
|---|---|
static Action |
printAction(JComponent component)
Returns the Print action for a component.
|
static Action |
printAction(PrintProvider[] providers)
Returns the Print action for the given print providers.
|
public static final String PRINT_NAME
public static final String PRINT_ORDER
Integer. All visible and
printable components are ordered and shown in the Print Preview
dialog from the left to right.public static final String PRINT_SIZE
Dimension.public static final String PRINT_PRINTABLE
Boolean.TRUE must be set as a client property of the component.public static Action printAction(JComponent component)
component - is the component being printedPrintProviderpublic static Action printAction(PrintProvider[] providers)
PrintPages returned by the providers are
shown in the Print Preview dialog.providers - is the array of print providersPrintProviderBuilt on August 24 2014. | Portions Copyright 1997-2014 Sun Microsystems, Inc. All rights reserved.