Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Using menus and menu bars in applets

How to use the AWT's built-in menu classes in applets

  • Print
  • Feedback

Menus and applets in the AWT

Java's Abstract Windowing Toolkit (AWT) includes four concrete menu classes: menu bars representing a group of menus that appears across the top of the window or the screen (class MenuBar); pull-down menus that pull from menu bars or from other menus (class Menu); menu items that represent menu selections (class MenuItem); and menu items that the user can turn on and off (class CheckBoxMenuItem).

These classes are all subclasses of MenuComponent, not subclasses of Component. Because they aren't components, they can't be placed in any container -- the way you would place buttons and lists in a container. In a graphical user interface (GUI) the only way to use these menu classes is to place a menu bar (which can contain additional menus) in a frame using the frame's setMenuBar method. Since the applet class is not a subclass of Frame, it does not inherit the setMenuBar method. This means you cannot simply place a menu bar in an applet.

Still, there are several ways to create applets with menus: (1) An applet can open a new frame that contains an AWT menu bar with pull-down menus -- perhaps in response to the user clicking on a button; (2) an applet can use a custom pop-up-menu class, either one that you implement yourself or one that comes from a third-party widget library. (The AWT itself does not include pop-up menus.); and (3) an applet can also use an AWT menu bar with pull-down menus embedded in its enclosing rectangle in a Web page. You can accomplish this last action using the simple technique described in this article.

While each of the three approaches has its uses, the last one has several advantages over the other two. The main advantage of this approach over a design in which the applet opens a new frame with a menu bar are:

  • An applet that does not open a new frame integrates better with Web pages and is less distracting to users than an applet that requires the user to click a button to open a frame. This is especially true in windowing environments that require the user to manually place the new frame -- like many X window managers.

  • Using AWT menus within an applet rather than opening a new frame allows you to embed an exact working copy of the GUI of a standalone application within a Web page -- or you can even embed the entire application. This capability can be valuable for user guides and tutorials, as well as for distributing applications to users who do not have a standalone Java interpreter installed on their machines. These users can use the application with a Java-enabled Web browser instead of launching the standalone application.


The main advantages of using AWT menus in applets rather than using a custom pop-up-menu class are:

  • Using a custom pop-up-menu class requires you to either develop the class, purchase it, or at least find a suitable free one, whereas the AWT menus are built into every Java run-time environment.

  • The AWT menus always have the look and feel of the native windowing environment, whereas a custom pop-up-menu widget might not.

  • An applet that uses the AWT menus loads faster because it does not need to load the menu class from across the network.


Placing a menu bar and menus in an applet

Although an applet is not a subclass of Frame and therefore cannot contain a menu bar, it is always contained within some frame. You can find the frame that contains your applet using the following code, which was suggested for a different purpose in the Recently Asked Questions document on Sun's JavaSoft site:

  • Print
  • Feedback

Resources