DevFaqHowToPrint

Help the Print menu item is disabled!

There are several ways to enable printing for a custom data:

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 from the DataObject of the Nodes. The StyledDocuments, returned by the editor cookies, 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.

See PrintManager javadoc for details.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo