NetBeans Platform Cookbook Chapter 10 02


Each application must provide print feature. NetBeans Platform has the Print API module for it. It provides the Print and Print Preview actions.


If you want inform the NetBeans Platform that your component is printable you have several ways: provide PrintProvider, declare component as printable or by EditorCookie or InputStream found in activated Node's Lookup.

How to

Declare component as printable

The simplest way is declaring the JComponent subclass within a TopComponent as printable by a client property:

        putClientProperty( PrintManager.PRINT_PRINTABLE, Boolean.TRUE); 

The name into header or footer is taken from name of the enclosing TopComponent. You can set it manualy:

        putClientProperty( PrintManager.PRINT_NAME, "Annual Results");

If the size of the printed area is different from visible component set it:

        putClientProperty( PrintManager.PRINT_SIZE, 
                           new Dimension(600, 800) );

If your visualized data are spread on several components in the TopComponent each of them can be declared as printable. You can set their order in the print sequence:

        putClientProperty( PrintManager.PRINT_ORDER, Integer.valueOf(1) );


Figure 10.1 Print top component by printable property.


Figure 10.2 Print data table by printable property.

Provide PrintProvider

The PrintProvider takes care about layout and print process. It is more difficult than others. Create it if you need provide your own specific printing process. You must implement three methods:

String getName() is used as a name of the document and is printed into header or footer.

Date lastModified() returns date of last modification.

PrintPage[][] getPages(int width, int height, double zoom) returns matrix of pages. The PrintPage class provides standard method print(Graphics g).


Figure 10.3 Print by PrintProvider

Let's Explain!

Now we describe the print process.

  1. First by PrintProvider - The print manager asks the Lookup of the active TopComponent if it contains any PrintProvider instance. If it is this provider is used for print preview.
  2. Second by printable property - If there is not any PrintProvider the print manager searches printable components walking through the components tree of the active TopComponent. Remember that printable is such a component we set the client property as described above. The print(Graphics g) method is used to create preview and to print each component.
  3. Through activated Nodes - If is not found any printable component the manager retrieves selected/activated Nodes of the active top component and looks up the EditorCookie or InputStream. EditorCookie provides the StyledDocument and the InputStream provides String with styled text. It is used next. The Platform makes textual documents printable by this way automaticaly – you can print source texts by default.

Try the print example' application. Notice differences: If you label the whole top component as printable the PrintManager renders whole top component spread into several paper pages in pages matrix. If you label the JTable as printable the table is printed similar. If do not want print gray components override the component's print method and set the background temporary.

Notes/Tips - PrintAction

The PrintAction for the component can be retrieved by factory method



        PrintManager.printAction(PrintProvider[] providers)

and added into menu or toolbar.

Notes/Tips - Documentation

Documentation links:

PrintManager class

PrintProvider class

Print API

Notes/Tips - Examples

To see and try examples go to examples into 10_best / printing directory. There are three top components simulating data search form. One top component is labelled as printable, the second has labeled only the table and the third provides its own PrintProvider.

Text and sources were created under NB 6.8, 6.9, 7.0, 7.1 and they need enhancing.


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