logo
  Sample Programs and Sample Code
logo

[Installation] [Print, PageSetup, and Print Preview Dialogs] [Custom Flowables]

J2PrinterWorks includes 32 sample programs with complete source code.  The source code for these applications may be freely used, modified, incorporated, and distributed without restriction as part of any software that uses J2PrinterWorks itself (see License and Terms of Use ).
 
 
J2PrinterWorksTestApplication
RUN ME FIRST!    A comprehensive test of all the J2PrinterWorks printing components
 

StylepadPrinter
Sun's Stylepad demo program (JTextPane-based word processor) with printing added
J2TextPrinterTestApplication
J2TextPrinter program that reads and prints HTML, RTF, and StyledDocument files
J2TextPrinterHTMLFormTest
Demonstrates use of "direct print" to print user entries in HTML <form> documents
J2TextPrinterSimplestTest
Minimum J2TextPrinter program that prints a JTextPane (using HTML)
 

J2TablePrinterTestApplication
J2TablePrinter program that reads a file directory and prints it as a JTable
J2TablePrinterSimplestTest
Minimum J2TablePrinter program that prints a JTable
 

J2TreePrinterTestApplication
J2TreePrinter program that reads a file directory and prints it as a JTree
J2TreePrinterSimplestTest
Minimum J2TreePrinter program that prints a JTree
 

J2ListPrinterSimplestTest
Minimum J2ListPrinter program that prints a JList



J2PanelPrinterImageApplication
J2PanelPrinter program that reads and prints any .gif or .jpg image file
J2PanelPrinterGraphicsTest
Minimum J2PanelPrinter program that prints a Graphics drawing and/or Image
J2PanelPrinterSimplestTest
Minimum J2PanelPrinter program that prints a JLabel in a JPanel
 

J2ComponentPrinterSimplestTest
Minimum J2ComponentPrinter program that prints a Component
J2ComponentPrinterTestApplication
Reimplementation of  J2PrinterWorksTestApplication using only J2ComponentPrinter



J2FlowPrinterSimplestTest
Minimum J2FlowPrinter program that prints a JTextPane, JTable, JTree, JList, and JPanel back-to-back
J2FlowPrinterTestApplication
J2FlowPrinter program that prints a typical report made up of JTextPane, JTable, JTree, JPanel, etc.
   
J2Printer14PrinterSelectionTest
J2Printer14 program that lets you find and choose printers (JDK 1.4.x or later required)
J2Printer14CrossPlatformTest
J2Printer14 program that uses Java cross-platform printing dialogs (JDK 1.4.x or later required)
J2Printer14PSTest
J2Printer14 program that prints to a Postscript file (JDK 1.4.x or later required)



J2Printer14SimplePrintDialogJ2Printer14 program that implements a basic print dialog (JDK 1.4.x or later required)
J2Printer14TabbedPrintDialogJ2Printer14 program that implements a tabbed print dialog (JDK 1.4.x or later required)
J2Printer14PrintPlusPreviewDialogJ2Printer14 program that implements a combined print and print preview dialog (JDK 1.4.x or later required)
J2Printer14SimplePageSetupDialogJ2Printer14 program that implements a basic page setup dialog (JDK 1.4.x or later required)
J2Printer14TabbedPageSetupDialogJ2Printer14 program that implements a tabbed page setup dialog (JDK 1.4.x or later required)
J2PrinterWorksSimplePrintPreviewDialogProgram that implements a basic print preview dialog
J2PrinterWorksFullPrintPreviewDialogProgram implements the standard J2PrinterWorks full-featured print preview dialog
J2PrinterPDFTest
Program that prints to a PDF file +  display & print any PDF file (iText.jar and Acrobat Reader required)



J2PrintToImageTest
Program that tests printing a page to an Image and saving Image as a .jpg file



J2PrinterWorksApplet

Simple unsigned printing applet.      Run it now
J2Printer14Applet

Simple J2Printer14 unsigned printing applet (JDK 1.4.x or later required)      Run it now



J2PrinterWebStartTest

J2PrinterWebStart program that tests printing from a Java Web Start application

 

Installing and running sample programs

J2PrinterWorks is a Java 2 program.  You must have Java 2 (JDK 1.2.x or later) installed on your system.  JDK 1.4.x or later is highly recommended.  You may encounter performance problems, feature limitations, and possible malfunctions when printing under JDK 1.2.x or JDK 1.3.x due to various problem present in earlier Java systems (see Known Problems section). Information is available at the Sun Java site for how to download (free) and install the latest release of Java .

All the sample programs are included in the J2PrinterWorks distribution (.zip) file.  Unzip this file to create folders containing all the J2PrinterWorks sample programs.

In the top level J2PrinterWorks installation folder, you will find a folder called samplePrograms.  Inside this folder, you will find multiple folders containing all the J2PrinterWorks sample programs and source code files.  Most of the sample applications (all but the last three sample programs in the above list) are double-clickable .jar files.

To run the sample applets, double-click on the corresponding .html file. 

To run the J2PrinterWebStartTest, you need to build (e.g. using Ant) and deploy to a Java-enabled web server (e.g. Tomcat).  Instructions for doing this are included in the readme.txt file included in the J2PrinterWebStartTest folder and are the same as the standard Java Web Start sample programs from Sun.

In each folder other than J2PrinterWebStartTest there is a build.bat Windows batch file for building the sample program from the source code, and a run.bat Windows batch file for running the sample program.  Alternatively, you can open a command window ("Terminal" on the Mac, the shell window in Unix, or MS-DOS window in Windows), and type the command lines such as the following:: 

java -cp .;..\..\J2PrinterWorks.jar J2PrinterWorksTestApplication

NOTE: On Unix and Mac, the class path file separator is colon instead of semi-colon and the path separator is forward slash instead of backward slash.

Certain of the above sample programs are capable of throwing an Out of Memory exception if very large documents are displayed and printed or when displaying a print preview dialog in a 2 page display at maximum zoom.  This is because the default Java heap size is very small (16MB) and printing generally consumes large amounts of memory (you may well encounter the same problem with your own substantive applications).  The solution is to increase the available Java heap size by adding the option -mxNm to your java execution command line, where N = 1024 would allow Java to ask for up to 1024 MB of memory for its heap.  See the documentation section FAQ, Miscellaneous, Q4 for further details.


Print, Page Setup, and Print Preview Dialogs

J2PrinterWorks supports the native and cross-platform print dialogs and the native and cross-platform page setup dialogs that are provided by Java.  In addition, J2PrinterWorks contains full source code for three print dialogs, two page setup dialogs, and two print preview dialogs that may be freely modified by J2PrinterWorks developers to create dialogs suitable for their own purposes.  There are seven corresponding sample programs (see above) that demonstrate the use of each of these J2PrinterWorks dialogs.  The J2PrinterWorks print, page setup, and print preview dialogs source code files are:

SimplePrintDialog.java - contains a basic print dialog for selecting the printer, page range, and number of copies (requires J2Printer14 and JDK 1.4 or later).

TabbedPrintDialog.java - contains a print dialog with three tabbed sections: "General" for selecting the printer, page range, and number of copies, "Page Setup" for specifying paper size, orientation, and margins, and "Headers/Footers" for specifying left, center, and right headers and footers (requires J2Printer14 and JDK 1.4 or later).

PrintPlusPreviewDialog.java - contains a print dialog for selecting the printer, page range, number of copies, paper size, orientation, and margins, combined with an integrated print preview viewer (requires J2Printer14 and JDK 1.4 or later).

SimplePageSetupDialog.java - contains a basic page setup dialog for specifying paper size, orientation, and margins (requires J2Printer14 and JDK 1.4 or later).

TabbedPageSetupDialog.java - contains a page setup dialog with two tabbed sections: "Page Setup" for specifying paper size, orientation, and margins, and "Headers/Footers" for specifying left, center, and right headers and footers (requires J2Printer14 and JDK 1.4 or later).

SimplePrintPreviewDialog.java - contains a basic print preview dialog which displays one page with minimal controls.

PrintPreviewDialog.java - contains the source code for a full-featured print preview dialog and which is the implementation of the standard J2PrinterWorks print preview dialog invoked by the J2Printer method showPrintPreviewDialog().

In addition, several other J2PrinterWorks sample programs provide source code for so-called "detailed" page setup dialogs which developers may wish to use or adapt to provide control over certain J2PrinterWorks features.  These page setup dialogs are not as commercial polished as the above page setup dialogs but are useful in showing how to control specific J2Printer class printing features and the features of the J2PrinterWorks printing components J2TextPrinter, J2TablePrinter, etc., including how the unique properties of these components can be displayed and modified.  These special-purpose "detailed" page setup dialogs are:

J2PrinterWorksPageSetupDialog.java - contains the controls for the standard J2Printer class features including headers/footers contents, style, and gaps, margins, print and print preview scale factors, orientation, print thread, print dialog, number of copies, and print job name.

J2PrinterSettingsDialog.java - contains a minimum subset of the standard J2Printer class features without features such as orientation or margins, which therefore avoids overlap with the standard Java page setup dialogs and could be used in addition to them.

 J2TextPrinterPageSetupDialog.java - contains the standard J2Printer features plus controls specific to J2TextPrinter including centering and WYSIWYG control.

J2TablePrinterPageSetupDialog.java - contains the standard J2Printer features plus controls specific to J2TablePrinter including centering, outside line control, column header replication and row/column ranges.

J2TreePrinterPageSetupDialog.java - contains the standard J2Printer features plus controls specific to J2TreePrinter including centering and outside line control


Custom Flowables

The J2PrinterWorks Flowable interface is used to implement objects which are able to print incrementally, advancing by one rectangular area at a time.  Flowables are used by J2PrinterWorks to implement printing components that can span multiple pages, but unlike Pageables, they can also begin and end in the middle of any page.  The J2PrinterWorks abstract base class J2Pageable takes any such Flowable and provides an implementation of a full Java Pageable complete with headers, footers, margins, orientation, centering, scaling, etc.  See sections Pageable and Flowable, Class Diagram, and FAQ. 

The J2PrinterWorks classes J2TextPrinter, J2TablePrinter, J2TreePrinter, J2ListPrinter, J2PanelPrinter, and J2FlowPrinter all work by implementing Flowable and inheriting Pageable from J2Pageable in the above manner.  J2PrinterWorks also provides a source code template FlowableExample.java which shows how you can do the same thing in order to create paginatable components of your own which can control how much they print on a page and thereby where pagination boundaries will occur.  This is useful if you have custom multi-page content to be printed, such as your own tables, drawings, page layouts, etc.


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