logo
  Utility Classes
logo

[HorizontalLine ] [VerticalGap ] [PageEject ] [J2Label ] [ImagePanel ] [PrintingEventHandler ]


J2PrinterWorks contains a number of special-purpose classes to help with printing tasks.

HorizontalLine

HorizontalLine is a class that implements a Flowable to be used for drawing a horizontal line separator between other Flowables in a J2FlowPrinter instance.  You can specify white space gap above/below line (in pixels), height of line (in pixels), width of line (in pixels wide or indented from edges), and Color of line. The white space gap below the line does not carry over page boundaries.

For example, when using J2FlowPrinter to print a sequence of Flowables, the sequence:

     flowPrinter.addFlowable(flowable1);
     flowPrinter.addFlowable(new HorizontalLine());
     flowPrinter.addFlowable(flowable2);
will insert a default horizontal line separator between flowable1 and flowable2.

HorizontalLine ()
          Construct a HorizontalLine object 4 pixels high with 16 pixels above and below (= 1/2 inch total), and width 72 pixels indented on each size (= 1 inch each), and color lightGray.

HorizontalLine (int gap, int height, int width, java.awt.Color color)
          Construct a HorizontalLine object with a given gap, height, width, and color
 

VerticalGap

VerticalGap is a class that implements a Flowable that introduces a specified vertical separation between other Flowables in a J2FlowPrinter instance.

For example, when using J2FlowPrinter to print a sequence of Flowables, the sequence:

     flowPrinter.addFlowable(flowable1);
     flowPrinter.addFlowable(new VerticalGap(1.0));
     flowPrinter.addFlowable(flowable2);
will cause a one inch vertical white space gap to appear between flowable1 and flowable2.

The vertical gap does not carry over page boundaries, so in the above sequence if pageable1 ends within one inch of the bottom of a page, then new VerticalGap(1.0) will use up the remaining space on that page, but flowable2 will start immediately at the top of the next page.  The specified vertical gap cannot be negative and the default gap size is .25 inches.

In addition, VerticalGap also has an optional "testGap" feature.  This provides way to test for the remaining space left on a page and skip to the top of the next page if not enough space remains.  This provides a way to prevent too small a portion of the next Flowable appearing at the bottom of a page (such as one line of a JTextPane, one row of a JTable, etc.).

For example, when using J2FlowPrinter to print a sequence of Flowables, the sequence:

     flowPrinter.addFlowable(flowable1);
     flowPrinter.addFlowable(new VerticalGap(1.5, 0.25));
     flowPrinter.addFlowable(flowable2);
will first determine how much space is left on the page after the end of flowable1.  If the remaining space is less than 1.5 inches, the VerticalGap will use up the rest of the space on this page and flowable2 will begin at the top of the next page.  If there is 1.5 inches or more remaining on this page, the VerticalGap will cause a vertical white space gap of .25 inches to appear between flowable1 and flowable2.  You can specify a test gap plus a regular gap of 0.0 if you only want to test for sufficient remaining space on the page.

VerticalGap ()
          Construct a VerticalGap object (default gap .25 inches)

VerticalGap (double gap)
          Construct a VerticalGap object with a given gap size in inches

VerticalGap
(double testGap, double gap)

          Construct a VerticalGap object with a test gap and regular gap size in inches (default testGap: 0.0 inches)

PageEject

PageEject is a class that implements Flowable that causes the next Flowable to start at the top of the next page in a J2FlowPrinter instance.

For example, when using J2FlowPrinter to print a sequence of Flowables, the sequence:

     flowPrinter.addFlowable(flowable1);
     flowPrinter.addFlowable(new PageEject());
     flowPrinter.addFlowable(flowable2);
will cause flowable2 to start on a new page.

PageEject ()
          Construct a PageEject object
 

J2Label

J2Label is a subclass of JLabel with a default Font of Serif,PLAIN,10, default foreground Color black, and default background Color white and opaque, useful for making HTML JLabels that work consistently across JDK 1.2, 1.3, and 1.4.  This class is particularly useful when used to specify a J2PanelPrinter instance, e.g.:

    J2PanelPrinter panelPrinter = new J2PanelPrinter(new J2Label("This is a label"));
or
    J2PanelPrinter panelPrinter = new J2PanelPrinter(new J2Label("<html>This is <b>a label</b></html>"));

This subclass is useful for defining JLabel instances when using HTML so that you can overcome a series of problems that make it difficult to use JLabel consistently across the different JDK versions. These problems include:
    Under JDK 1.3 and 1.3.1 the default foreground color is blue, under JDK 1.2, 1.4, and 1.5 it is black
    Under JDK 1.4 or later the default Font is BOLD, previously it was PLAIN
    Under JDK 1.4 or later, calling setFont controls the initial HTML font, previously it was ignored
    Under JDK 1.4 or later, the <font size=n> tag comes out the same as in browsers, previously it was one size too big

J2Label addresses these problems by always making the foreground Color black, and making the default font PLAIN, and under JDK 1.3 and 1.3.1 modifying the HTML to set the initial font. The default font is set to Serif,PLAIN,10, which is the same as the J2Printer default String font for headers and footers.

In addition, because of the <font size=n> problem, we recommend you specify all HTML fonts in relative size, e.g.
    <font size=+3> is approximately 36 point = <big><big><big>
    <font size=+2> is approximately 24 point = <big><big>
    <font size=+1> is approximately 18 point = <big>
    <font size=+0> is approximately 14 point
    <font size=-1> is approximately 12 point = <small>
    <font size=-2> is approximately 10 point = <small><small>
    <font size=-3> is approximately  8 point  = <small><small><small>
Using relative sizes for all HTML JLabel (and JTextPane) instances will help you make your output look the same under JDK 1.2.x, 1.3.x, 1.4.x, and 1.5.x.  Another option you may find useful is to use the HTML tags <big> and <small>.

The com.wildcrest.j2printerworks.J2Label class has the same six constructors as javax.swing.JLabel, the most general of which is:

J2Label (java.lang.String text, javax.swing.Icon icon, int horizontalAlignment)
          Same as JLabel but with a default Font of Serif,PLAIN,10, default foreground Color black, and default background Color white and opaque.
 

ImagePanel

ImagePanel is a JPanel subclass for displaying an Image. Sets preferred size to the size of the Image, and centers the Image within the actual size of the JPanel if different.  The Image can be minified of magnified according to a specified scale factor.  Java Borders are supported for ImagePanel.

ImagePanel (java.awt.Image image)
          Constructs a JPanel for displaying the specified Image

ImagePanel ()
          Constructs a JPanel with an empty Image

setImage (Image image)
          Specifies the Image to be displayed in the ImagePanel

setScale (double scale)
          Sets the scaling factor for drawing the Image

PrintingEventHandler

PrintingEventHandler is a convenience class that implements a PropertyChangeListener with stub (empty) methods for the printing events provided by J2PrinterWorks, with the exception of exceptionThrown which has a default implementation of e.printStackTrace().  You can instantiate PrintingEventHandler and override any or all of its methods to invoke code of your choosing. 

Here is an outline of how PrintingEventHandler is used (see "J2Printer" section of this documentation for more details):   

J2Printer printer = new J2Printer();
...
printer.addPropertyChangeListener(new MyPrintingEventHandler());
...
class MyPrintingEventHandler extends PrintingEventHandler {

      // include any or all of the following:

      public void printingStart() { your code here }
      public void printingDone() {
your code here }

      public void printDialogOK() { your code here }
      public void printDialogCanceled() {
your code here }

      public void pageSetupDialogOK() { your code here }
      public void pageSetupDialogCanceled() {
your code here }

      public void pageStart(int pageNum) { your code here }
      public void pageDone(int pageNum) { your code here }

      public void printPreviewStart(int pageNum) { your code here }
      public void printPreviewDone(int pageNum) { your code here }

      public void exceptionThrown(Exception e) { your code here } // default is e.printStackTrace()
}

Or, to listen for just one possible outcome, it is more compact to use an inner class.  For example, you can use the following code to detect when printing is done:

J2Printer printer = new J2Printer();
...
printer.addPropertyChangeListener(new PrintingEventHandler() {
      public void printingDone() { System.out.println("Done!"); }
});

Or to listen for possible thrown exceptions:

J2Printer printer = new J2Printer();
...
printer.addPropertyChangeListener(new PrintingEventHandler() {
      public void exceptionThrown(Exception e) {
            System.out.println("Got an exception: " + e);
            e.printStackTrace();
      }
});


© Copyright 2009, Wildcrest Associates (http://www.wildcrest.com )