Java Reference
In-Depth Information
If you recompile and try printing a sketch that is wider than it is long, it should come out automatically in
landscape orientation. Although everything works, there is a better way to implement printing. The Print-
erJob and PrintService objects are re-created every time the event handler executes. This has a detriment-
al effect on the performance of the printing event handler in the FileAction class, and what's more, it's not
necessary. You will fix this in the next section.
Improving the Printing Facilities
Of course, there are many situations where choosing the best orientation from a paper usage point of view
might not be what the user wants. Instead of automatically setting landscape or portrait orientation based on
the dimensions of a sketch, you could leave it to the user with a dialog to select the page setup parameters in
the print dialog that you display. The mechanism for the user to set the job parameters is already in place in
Sketcher. Clicking the Properties button on the dialog usually displays an additional dialog in which you can
set the parameters for the print job, including whether the output is portrait or landscape. Whatever is set in
the print dialog overrides the orientation that you determine programmatically in the actionPerformed()
method in the FileAction class.
However, the printing facilities in Sketcher are not always implemented in this way for an application.
The toolbar button and the File Print menu item do exactly the same thing, but often it's the menu item
that pops a print dialog, and the toolbar button just initiates printing of the current document. Also, there's
often a separate menu item that enables the page to be set up for printing, independent of the process of
printing a document. You can fix that without too much difficulty, though.
In the actionPerformed() method in the FileAction class, you need to be able to determine whether
it was the toolbar button or the menu item that initiated the event. You can get a reference to the object that
originated the event by calling the getSource() method for the ActionEvent object that is passed to the
actionPerformed() method. You can easily check whether the source object is of type JButton. If it is, it's
the toolbar button that originated the event, and if it isn't, it was the menu item.
You can now update the code in the actionPerformed() method in the FileAction inner class so you
get the print dialog displayed only when the menu item is selected:
// The view is the page source
printJob.setPrintable(theApp.getView(), pageFormat);
boolean printIt = true;
// If it's not a toolbar button...
if(!(e.getSource() instanceof JButton)) {
printIt = printJob.printDialog();
// ...display the print dialog
}
if(printIt) {
// If printIt is true...
try {
printJob.print(); // ...then print
} catch(PrinterException pe) {
System.out.println(pe);
JOptionPane.showMessageDialog(SketcherFrame.this,
"Error printing a sketch.",
"Printer Error",
Search WWH ::




Custom Search