Java Reference
In-Depth Information
JOptionPane.showMessageDialog(SketcherFrame.this,
"Error printing the application
window.",
"Printer Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
// Construct the file drop-down menu
// Code to add file menu items as before...
fileMenu.addSeparator(); // Add separator
fileMenu.add(printAction); // Print sketch
menu item
fileMenu.add(printSetupItem); // Print page setup
menu item
fileMenu.add(printWindowItem);
// Print window menu
item
fileMenu.addSeparator(); // Add separator
fileMenu.add(exitAction); // Print sketch
menu item
menuBar.add(fileMenu); // Add the file menu
}
Directory "Sketcher 13 printing the application window"
Note that you must qualify
this
in the
actionPerformed()
method. If you don't,
this
refers to the an-
onymous class object, and not the
SketcherFrame
object.
The application window is responsible for printing the window because it is obviously best placed to do
this, so you must make the
SketcherFrame
class implement the
Printable
interface. Change the first
line of the class definition to the following:
public class SketcherFrame extends JFrame
implements ActionListener, Observer,
Printable {
Directory "Sketcher 13 printing the application window"
Now you can add the definition of the
print()
method to the
SketcherFrame
class:
// Print the window
public int print(Graphics g,