Q: When you use system.out.println(), it prints to the screen. How do you print to the printer?
A: There are several ways of accomplishing this task in Java. Depending on your needs, it can be as simple as opening LPT1 as a file and using file I/O methods, using the PrintJob class and the printAll() method, or getting more formatting capabilities with a solution such as the Java 2 Printing API. Here’s a look at each technique.
Open LPT1
The first solution, treating LPT1 as a file, is an easy way to output text strings from within a program. However, you must exercise caution, as LPT1’s availability is dependent on the host environment. Once you access LPT1 successfully, you need to pay attention to text formatting. If the lines are too long, the text that falls off the side of the page is truncated (there is no line wrap). You must also output a form feed (\f) at the end of outputting the data you want printed. If the form feed is left off, the data will stay in the print buffer until another print job comes along.
Use printAll()
The second possible solution is to use the printAll() method of a component in conjunction with the PrintJob class. PrintJob is a part of the java.awt package and is responsible for starting and executing a print job. The programmer has no control over the print dialog—it displays automatically when you call the PrintJob method getGraphics. If the user cancels, null is returned. If the user selects OK, a valid graphics object is returned. This object is then used to draw to the print device. The dispose method of the graphics object causes a form feed on the paper and frees up resources.
The printAll() method was introduced with JDK 1.1 and provides an easy way to draw to the print device. The method will cause the component used as an argument and its subcomponents to be printed. Each component’s print method is called to perform the actual drawing. If you don’t need the whole hierarchy printed, just call the printAll() method of the particular component you are interested in.
Printing API
The final solution is to use the Java 2 printing API. This is accomplished via the PrinterJob class. It is used to set up a print job, display a print dialog (if you want), and then print the pages of a job. PrinterJob is part of the java.awt.print package.
The Printable interface of PrinterJob defines how each page is drawn or painted via the print method. If you are printing a document with pages of different formats (e.g., a cover page and the table of contents), each format must have its own print method. The application tells PrinterJob how to print via setPrintable (all pages, one format) or setPageable (multiple pages, different formats).
An important item to note is that the print method may be called more than once. The clip region of the graphics parameter is set for the area of the page that is to be drawn. In the given example, all items are drawn regardless of the clip value. If the text falls outside of the clip area, nothing is rendered. The text output appears only once since it falls within a given clip region one time. If you want to be efficient (and help performance), you should modify the code to render only the areas that fall within the given clip region.
Listing A shows a sample code listing.
Do you have a Java question?
If you need Java answers, Builder.com’s technical staff can help. Send us your Java question, and we’ll take a crack at it.