Java Reference
In-Depth Information
// Element menu items
popup.add(new JMenuItem(lineAction));
popup.add(new JMenuItem(rectangleAction));
popup.add(new JMenuItem(circleAction));
popup.add(new JMenuItem(curveAction));
popup.add(new JMenuItem(textAction));
popup.addSeparator();
// Color menu items
popup.add(new JMenuItem(redAction));
popup.add(new JMenuItem(yellowAction));
popup.add(new JMenuItem(greenAction));
popup.add(new JMenuItem(blueAction));
}
Directory "Sketcher 5 displaying a font dialog"
This adds the element menu items to the pop-up. You can call this method from the SketcherFrame con-
structor:
public SketcherFrame(String title, Sketcher theApp) {
setTitle(title); // Set the window title
this.theApp = theApp; // Save app. object reference
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // Default is exit the application
createFileMenu(); // Create the File menu
createElementMenu(); // Create the element menu
createColorMenu(); // Create the element menu
JMenu optionsMenu = new JMenu("Options"); // Create options menu
optionsMenu.setMnemonic('O'); // Create shortcut
menuBar.add(optionsMenu); // Add options to menu bar
createPopupMenu(); // Create popup
// Rest of the constructor code as before...
}
Directory "Sketcher 5 displaying a font dialog"
You might want to add the font choice menu item to the pop-up, but you must not try to add the same
JMenuItem object to two different menus. You could create an Action object that would display the font
dialog and create two menu item objects from that. Alternatively, you could create another menu item that
did the same as the original and add that to the pop-up. The former approach would be better, because a
single Action object would handle events from either menu item.
Displaying a Pop-Up Menu
Search WWH ::




Custom Search