Learning Java
Chapter 14: Using Swing Components
Menus Cont.
Creating menus is pretty simple work. You create a JMenu object, specifying the menu's title. Then you just add JMenuItems to the JMenu. You can also add JMenus to a JMenu; they show up as submenus. This is shown in the creation of the
Utensils menu:
JMenu utensils = new JMenu("Utensils");
utensils.setMnemonic(KeyEvent.VK_U);
utensils.add(new JMenuItem("Fork"));
utensils.add(new JMenuItem("Knife"));
utensils.add(new JMenuItem("Spoon"));
JMenu hybrid = new JMenu("Hybrid");
hybrid.add(new JMenuItem("Spork"));
hybrid.add(new JMenuItem("Spife"));
hybrid.add(new JMenuItem("Knork"));
utensils.add(hybrid);
In the second line, we set the mnemonic for this menu using a constant defined in the KeyEvent class.
You can add those pretty separator lines with a single call:
utensils.addSeparator( );
The
Quit menu item has some bells and whistles we should explain. First, we create the menu item and set its mnemonic, just as we did before for the
Utensils menu:
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.setMnemonic(KeyEvent.VK_Q);
Now we want to create an accelerator for the menu item. We do this with the help of a class called KeyStroke:
quitItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
Finally, to actually do something in response to the menu item, we register an action listener:
quitItem.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) { System.exit(0); }
});
Our
action listener exits the application when the
Quit item is selected.
Creating the
Spices menu is just as easy, except that we use JCheckBoxMenuItems instead of regular JMenuItems. The result is a menu full of items that behave like checkboxes.
The next menu,
Cheese, is a little more tricky. We want the items to be radio buttons, but we need to place them in a ButtonGroup to ensure they are mutually exclusive. Each item, then, is created, added to the button group, and added to the menu itself.
The final step is to place the menus we've just created in a JMenuBar. This is simply a component that lays out menus in a horizontal bar. We have two options for adding it to our JFrame. Since the JMenuBar is a real component, we could add it to the content pane of the JFrame. Instead, we use a convenience method called setJMenuBar( ), which automatically places the JMenuBar at the top of the frame's content pane. This saves us the trouble of altering the layout or size of the content pane; it is adjusted to coexist peacefully with the menu bar.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.