Java Reference
In-Depth Information
You can query the object that is returned for information about the capabilities of the printer and the kinds of
documents it can print but we won't divert down that track for the moment. One point you should keep in
mind is that sometimes there may not be a printer available on the machine on which your code is running.
In this case the
getPrintService()
method will return
null
, so it's a good idea to call the method and
test the reference returned, even if you don't want to obtain details of the printer.
If there are multiple print services available, such as several printers or perhaps a fax capability, you
can obtain an array of
PrintService
references for them by calling the static
lookupPrintServices()
method in the
PrinterJob
class. For example:
PrintServices[] printers = PrinterJob.lookupPrintServices();
The
printers
array will have one element for each print service that is available. If there are no print
services available, the array will have zero length. If you want to select a specific printer for the
PrinterJob
object to work with, you just pass the array element corresponding to the print service of
your choice to the
setPrintService()
method for the
PrinterJob
object. For example:
if(printers.length>0)
printJob.setPrintService(printers[0]);
The
if
statement checks that there are some print services before we attempt to set the print service. Without
this we could get an
IndexOutOfBoundsException
exception if the
printers
array has no elements.
Displaying a Print Dialog
When you want to provide the user with control over the printing process, you can display a print
dialog by calling the
printDialog()
method. This displays the modal dialog that applies to your
particular print facility. There are two versions of the
printDialog()
method for a
PrinterJob
object. The version without arguments will display the native dialog if the
PrinterJob
object is
printing on a native printer, so we'll look at that first, and return to the other version later.
If the dialog is closed using the button that indicates printing should proceed, the
printDialog()
method will return
true
, otherwise it will return
false
. The method will throw an exception of type
HeadlessException
if there is no display attached to the system. Thus to initiate printing you can
call the
printDialog()
method and, if it returns
true
, call the
print()
method for the
PrinterJob
object. Note that the
print()
method will throw an exception of type
PrinterException
if an error in the printing system causes the operation to be aborted.
Of course, the
PrinterJob
object can have no prior knowledge of what you want to print so you have
to call a method to tell the
PrinterJob
object where the printed pages are coming from before you
initiate printing. The simplest way to do this is to call the
setPrintable()
method, and pass a
reference to an object of a class that implements the
Printable
interface as the argument.
In Sketcher, the obvious candidate to print a sketch is the
SketchView
object (a reference to which is
stored in the
view
member of the application object). Thus we could allow sketches to be printed by
making the
SketchView
class implement the
Printable
interface. That done, we could then set the
source of the printed output just by passing a reference to the view to the
setPrintable()
method
for a
PrinterJob
object. You might consider the
SketchModel
object to be a candidate to do the
printing, but printing is not really related to a sketch, any more than plotting or displaying on the screen
is. The model is the input to the printing process, not the owner of it. It is generally better to keep the
model dedicated to encapsulating the data that represents the sketch.