dsto.dfc.swing.controls
Class PrintUtility

java.lang.Object
  extended by dsto.dfc.swing.controls.PrintUtility
All Implemented Interfaces:
Printable

public class PrintUtility
extends Object
implements Printable

A simple utility class that lets you very simply print an arbitrary component. Just pass the component to the PrintUtility.printComponent. The component you want to print doesn't need a print method and doesn't have to implement any interface or do anything special at all. If you are going to be printing many times, it is marginally more efficient to first do the following: PrintUtility printHelper = new PrintUtilities (theComponent); then later do printHelper.print (). But this is a very tiny difference, so in most cases just do the simpler PrintUtilities.printComponent (componentToBePrinted). Taken from 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/ May be freely used or adapted.

Version:
$Revision$
Author:
Luke Marsh

Field Summary
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
PrintUtility(Component componentToBePrinted, PageFormat pageFormat)
          Initializes variables.
 
Method Summary
static void disableDoubleBuffering(Component c)
          The speed and quality of printing suffers dramatically if any of the containers have double buffering turned on.
static void enableDoubleBuffering(Component c)
          Re-enables double buffering globally.
 void print()
          Opens up a print dialog and prints if the user clicks ok.
 int print(Graphics g, PageFormat myPageFormat, int pageIndex)
          Draws the component to be printed.
static void printComponent(Component c, PageFormat pageFormat)
          Calls the print method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintUtility

public PrintUtility(Component componentToBePrinted,
                    PageFormat pageFormat)
Initializes variables.

Parameters:
componentToBePrinted - The component that is to be printed.
pageFormat - Used to obtain the current PageLayout settings stored in the page format.
Method Detail

printComponent

public static void printComponent(Component c,
                                  PageFormat pageFormat)
Calls the print method.

Parameters:
c - The component to be printed.
pageFormat - Used to obtain the current PageLayout settings stored in the page format.

print

public void print()
Opens up a print dialog and prints if the user clicks ok.


print

public int print(Graphics g,
                 PageFormat myPageFormat,
                 int pageIndex)
Draws the component to be printed.

Specified by:
print in interface Printable
Parameters:
g - The Graphics object.
myPageFormat - The current page format.
pageIndex - The current page index.
Returns:
whether the page exists or not.

disableDoubleBuffering

public static void disableDoubleBuffering(Component c)
The speed and quality of printing suffers dramatically if any of the containers have double buffering turned on. So this turns if off globally.

See Also:
enableDoubleBuffering(java.awt.Component)

enableDoubleBuffering

public static void enableDoubleBuffering(Component c)
Re-enables double buffering globally.



Copyright © 2008 Commonwealth of Australia