Java Reference
In-Depth Information
JOptionPane.ERROR_MESSAGE);
}
}
Directory "Sketcher 9 Printing landscape with toolbar and menu different"
The value of the printIt flag you have defined is true by default. It may be reset to false by the value
returned by the printDialog() method, which is the case if the user cancels the dialog. The printDia-
log() method only gets called when the menu item caused the event for printing, not when the toolbar but-
ton is clicked.
If you've completed all the changes I've described, recompile Sketcher and run it again. You should now
get different behavior depending on whether you use the toolbar button or the menu item for printing.
Implementing Page Setup
You can add a menu item to the File menu to provide for setting up the printer page. Because you are not
going to add a toolbar button for this, you don't need to use an Action object. Action objects carry more
overhead than a simple component, so it's best not to use them unless you need the capability they provide.
Although this is somewhat inconsistent with the way you have created the rest of the File menu, you also
see how you can combine both techniques.
Add the boldfaced code to the createFileMenu() method in the SketcherFrame class to create the
JMenuItem object for the new printSetupItem menu item and add the menu item to the File menu:
private void createFileMenu() {
JMenu fileMenu = new JMenu("File"); // Create File menu
fileMenu.setMnemonic('F'); // Create shortcut
createFileMenuActions(); // Create Actions for File menu
item
// Create print setup menu item
JMenuItem printSetupItem = new JMenuItem("Print Setup...");
printSetupItem.setToolTipText("Setup the page for the printer");
printSetupItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// update the page format
pageFormat = printJob.pageDialog(pageFormat);
}
});
// Construct the file drop-down menu
fileMenu.add(newAction); // New Sketch menu item
fileMenu.add(openAction); // Open sketch menu item
fileMenu.add(closeAction); // Close sketch menu item
fileMenu.addSeparator(); // Add separator
fileMenu.add(saveAction); // Save sketch to file
fileMenu.add(saveAsAction); // Save As menu item
fileMenu.addSeparator(); // Add separator
fileMenu.add(printAction); // Print sketch menu item
Search WWH ::




Custom Search