Java Reference
In-Depth Information
The print job is started by calling method print of PrinterJob :
printJob.print();
This method throws a PrinterException and is therefore embedded into a try -
catch block.
To tell the print job which component it has to print we use setPrintable .
The argument of this method has to be a Printable object, i.e. an object that
implements the Printable interface:
setPrintable(Printable printableObject)
The print job is sent to the 'default printer' specified on the computer. If one wishes
to select another printer, one calls method
boolean printDialog()
Then a printer selection dialogue similar to the one shown in Figure 19.2 appears.
There one can select a number of options, e.g. the printer, the number of copies to
print or the paper orientation. On closing the dialogue a boolean value is returned.
If the value is true , then the print job is processed, otherwise it is cancelled.
The following code snippet shows the application of class PrinterJob to print
printableComp :
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(printableComp);
if (printJob.printDialog()){
try {
printJob.print();
Figure 19.2 A printer selection dialogue
Search WWH ::




Custom Search