|
J2TreePrinter
Overview
J2TreePrinter is a Java 2 component for printing multi-page JTree documents. J2TreePrinter converts any JTree or JTree subclass into a Java Pageable, suitable for printing by the J2Printer class (or any Java PrinterJob). It also implements Flowable for any JTree, so that it may be used with J2FlowPrinter as part of a series of Flowables printed back-to-back.
J2TreePrinter Pagination
A J2TreePrinter instance will print a JTree as a Pageable. J2TreePrinter paginates JTree instances across multiple pages vertically, breaking the JTree on row (tree node) boundaries. A JTree that is too wide horizontally will be rescaled to fit the page width. The JTree can be left, right, or center justified horizontally, and top, bottom, or center justified vertically. You can also specify whether to draw an outside line around the JTree.
Programming
You will find it useful to begin your program with:
import com.wildcrest.j2printerworks.*;
rather than spell out the full package name for all J2PrinterWorks classes.
J2TreePrinter can instantiated with a zero-argument constructor:
J2TreePrinter treePrinter = new J2TreePrinter();
This creates a J2TreePrinter object and initializes all values to their defaults (see the J2TreePrinter Javadoc documentation ). To specify the JTree to be printed by J2TreePrinter, you can use the J2TreePrinter method setTree:
treePrinter.setTree(yourJTree);
At this point treePrinter is a Pageable suitable for printing by J2Printer.
Alternatively, you can use the single-argument J2TreePrinter constructor:
J2TreePrinter treePrinter = new J2TreePrinter(yourJTree);
The following is a simple but complete Java program (J2TreePrinterSimplestTest.java ) that displays and prints a JTree using J2TreePrinter:
import javax.swing.*;
import com.wildcrest.j2printerworks.*;
class J2TreePrinterSimplestTest{
static public void main(String args[]){
JTree tree = new JTree(); // has default content
for (int i=0; i<tree.getRowCount(); i++) tree.expandRow(i);
JFrame frame = new JFrame("J2TreePrinter test");
frame.getContentPane().add(tree);
frame.setSize(200,350);
frame.setVisible(true);
J2Printer printer = new J2Printer();
printer.setSeparatePrintThread(false);
J2TreePrinter treePrinter = new J2TreePrinter(tree);
printer.addPageable(treePrinter);
printer.print();
System.exit(0);
}
}
Most of the methods of J2TreePrinter are set and get methods for controlling its property values. The full list of J2TreePrinter methods, what they do, and their default values are given in the J2TreePrinter Javadoc documentation.
General properties
Whether J2TreePrinter is used as a Pageable or a Flowable, you can control whether it is left, right, or center justified horizontally on a page, top, bottom, or center justified within the remaining space on a page, and whether to draw an outside line around the JTree.
Horizontal and vertical alignment
J2TreePrinter will print your JTree either left, center, or right justified horizontally on the page between the left and right margins. The default value for horizontal alignment is CENTER.
Similarly, J2TreePrinter will print your JTree either top, center, or bottom justified vertically on the page between the gap below the header and gap above the footer. The default value for vertical alignment is TOP.
setHorizontalAlignment (int horizontalAlignment)
Sets the horizontal alignment (LEFT, CENTER, RIGHT) for printing the JTree on the page.
setVerticalAlignment (int verticalAlignment)
Sets the vertical alignment (TOP, CENTER, BOTTOM) for printing the JTree on the page.
»
|