SmartJReport - (JTree - Print, PDF, TIFF, PostScript, PNG, BMP, GIF, JPG, JPEG and more)

Visit our website and learn more about SmartJReport that supports all of your Java report, print, PDF and conversion requirements.

Print, PDF, Preview JTree - The Demo

This demo sample shows how to Print, PDF or simply preview an existing JTree. You can "silent print your JTree" using the JavaTreePrinter methods which you can learn looking the at the sample.

In the downloaded archive you will see some sample batch programs (e.g. JTreePrintDemo.bat, and more). For each batch file there is a corresponding JAVA sample program made available as part of the downloaded archive. Run the "JTreePrintDemo.bat" for example to see the following window with a JTree and few buttons to "Print", "PDF" and "Preview" the JTree. You will also see a button to let you choose a different directory root to be loaded as the new JTree.

This sample shows a JTree expanded only a little bit.

Yet another view of JTree demo sample program showing a lot of nodes are expanded and some of the nodes has files of very long name. Since it lets you FIT the content within the page width you will able to see the full tree without loosing anything.

Print JTree

You can either "silent print your JTree" or provide a UI control such as a Button as demonstrated in this sample program and print. Depending on the arguments to the JavaTreePrinter.print() method you show a "printer dialog" or print without showing a print dialog. In this sample we show a "print dialog" to let user select a printer, paper, orientation, copies, fit to printable area and others as seen in the print dialog next.

Print JTree - Fit to Page Width

Printer class JavaTreePrinter lets you set a FIT flag so the output may be fit to the page width. It is better to always set the flag so it can fit to page width applying compression if necessary. Available fit options are AbstractPrinter.PAGE_SCALING_NONE (no fit) and AbstractPrinter.PAGE_SCALING_FIT_TO_PRINTABLE_AREA (fit to page width) applied using setPageScaling(int) method. In the print dialog there is a check box "Fit to Printable Area" is equivalent to the above.


The report engine can let you convert the JTree report pages to a PDF document. You can convert JTree to PDF completely silently by getting a Pageable from JavaTreePrinter.Pageable() and then passing the Pageable to the ConversionFactory.saveAsPdf(Pageable) method. Following are some sample PDF created from Swing JTree.

  • JTree to PDF (no fit)
  • JTree to PDF (fit to page width)
  • Preview JTree

    It lets you preview JTree report in a preview window. JavaTreePrinter class has few preview() methods to call for showing the preview window. This demo chooses to show a paper selection dialog before showing the preview window. It is also important to remember that the preview window has options for the end user to Print, PDF and conversion to images. Preview window also lets you zoom in and out in order to control number of pages to output.

    Select paper size and orientation from this "Paper Selection" window.

    Here is how the Print Preview window looks like. It shows JTree Preview pages one at a time and you can browse them using the controls in the tool bar.

    JTree to PNG, BMP, GIF and JPEG/JPG

    Look at the "" and other conversion sample in the downloaded archive to learn about how.

    There are ways to convert JTree report pages to many different image formats such as PNG, BMP, GIF and JPEG/JPG. If you want to convert JTree to images silently we suggest you to get a Pageable (each page of which is an AbstractPage) form the JavaTreePrinter class and then pass the Pageable to the ConversionFactory.saveAsBMP(AbstractPage) (BMP), ConversionFactory.saveAsPNG(AbstractPage) (PNG), ConversionFactory.saveAsGIF(AbstractPage) (GIF), ConversionFactory.saveAsJPEG(AbstractPage) (JPEG), ConversionFactory.saveAsJPG(AbstractPage) (JPG) to convert to images. Conversion APIs provides the option to pass a resize factor so the output images size can be enlarged or shrinked.

    This demo sample lets you preview the JTree report pages. From the preview window you can convert one or more of the JTree report pages to images.

    JTree to TIFF

    This is similar to the above. Get the Pageable (convert to AbstractPage[] array) and call ConversionFactory.saveAsTIFF(AbstractPage[]) to produce the TIFF report.

    Also look at demo sample in the downloaded archive.

    JTree to Post Script

    Get the Pageable and create an array of AbstractPage[] then call ConversionFactory.saveAsPostScript(AbstractPage[]) to produce the Post Script output of your JTree report.

    Also look at demo sample in the downloaded archive.