Java Reference
In-Depth Information
Note that you also use an overloaded version of the print() method for the PrinterJob object to which
you pass the print request attribute set. Thus the print operation uses whatever attributes were set or modi-
fied in the print dialog displayed by the printDialog() method.
As you are using print attributes when printing, you should make the page dialog that is displayed in re-
sponse to the “Print Setup . . . ” menu item use them, too. There's a slight complication in that the behavior
of the pageDialog() method that has a parameter of type PrintRequestAttributeSet is different from
that of the method with a parameter of type PageFormat when the Cancel button in the dialog is clicked. The
former version returns null , whereas the latter version returns a reference to the PageFormat object that was
passed as the argument. If you don't take account of this, canceling the page setup dialog causes printing to
fail, because the pageFormat field in SketcherFrame is set to null . You therefore must modify the code
in the actionPerformed() method in the anonymous ActionListener class for the printSetupMenuItem
menu item. This is in the createFileMenu() method in SketcherFrame :
public void actionPerformed(ActionEvent e) {
PageFormat pf = printJob.pageDialog(printAttr);
if(pf != null) {
pageFormat = pf;
// update the page format
}
}
}
Directory "Sketcher 11 using the Java print dialog"
Now you update pageFormat only when the value returned by the pageDialog() method is not null .
If you run Sketcher with these changes you should see a dialog with three tabs when you print a sketch,
similar to that shown in Figure 21-13 .
FIGURE 21-13
 
 
Search WWH ::




Custom Search