Home
Username:
Password:
Java 6 Tutorials

User Interface / Menu

Subtitles of the Movie

Most applications have a menu, even if there are no menus on the secondary windows, there is usually a menu on the main window. A menu is simply a bar across the top of the window with a few pull downs. You construct each piece of the menu separately and then hook them all together. This program is an example of one with a simple menu. You need to import the JMenuBar, which is the bar which holds the pull downs. The JMenu class, which is the pull-down portion and the JMenu item, which is a menu push button. You will need to implement the action listener interface so you can receive events from the mouse clicking on the menu. Oh, by the way, I've done something a little different in this example. You may remember that in previous examples I used an anonymous class to receive the event of the window closing. Here the call to set default close operation does the same thing. When the window is closed, the application closes. Now, whenever I construct a menu, I do so in a separate method. That's because I have found that having all the menu code I selected in one place is very handy. It makes things easy to find when the need comes to make a change to the menu and believe me, menus change a lot. This method returns the JMenuBar object, which is to be placed across the top of the window. The call to the JFrame method set JMenuBar installs the menu at the top of the JFrame. This is the method that builds the menu. It begins by constructing the menu bar itself. This is the first pull-down. By convention, the first pull-down is named file. Notice the call to set lightweight pop-up enable to false? If you don't do this, the pull-down menu will actually come up behind some of the components on your window and not be visible. The add method is called to insert this pull-down as the first member of the menu bar. The creation of the JMenu item is the creation of a menu button. This example uses only text, but you can use an icon just like you can with any button. This object is added to the button as its action listener and the button is added to the menu. Two more buttons are constructed and added to the menu. They will appear in the order that they are added to the menu. In this example, a second pull-down is added to the menu bar. It will positioned directly to the right of the one that's already there. Finally, a couple of buttons are added to the new pull-down. This is a method that's called with each menu button. This example simply prints out the text from the button. It responds only to the exit button by closing the application. Here's how it works. You can see the menu here exactly as it was defined. In the next lesson, I will show you how you can use components other than simply push buttons inside menus.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available