Since the introduction of the graphical user interface (GUI), and even before with some text-based interfaces, menus have been an important part of software applications.
They allow you to easily locate application functions, instead of memorizing cryptic sequences of control, alt, shift and letters. Perhaps I’m showing my age, but I still remember the days of WordPerfect and Lotus 1-2-3, when even the simplest task required complex keystroke combinations. So popular are menus, that almost every Java programmer will have created a GUI application that uses menus at some point in time — their existence is almost mandatory. However, a special type of menu is often underutilized, and sometimes even forgotten by developers: the pop-up menu.
One of the nicest application features for advanced users is the ability to right-click on an application, and access a list of frequently accessed functions through a pop-up menu. Sure, you can click on the menu bar, search for the option you want which may be several menu levels deep, and then select it. The pop-up menu, however, is easier to use and faster. Professional applications use them, so why not Java applets and applications?
Examining Menus and Pop-up Menus
Before we begin by creating a pop-up menu in Java, it’s important that we are all thinking on the same wavelength. What exactly is a pop-up menu, and how does it differ from a standard menu?
Let’s start by examining something that everyone, even users, would find familiar. Figure 1 shows a fairly standard Java window, with a menu located at the top of the frame. You’ll notice that the ‘Edit’ menu has been selected, to reveal a drop-down menu with several items. Users can click on options, or even toggle settings such as a
Now consider the difference with the applet shown in Figure 2. This applet is running within a browser, and not as a framed GUI application. There are no menu bars, yet a menu does exist. This is an example of a pop-up menu, which is triggered in response to a user action (usually a right-click of the mouse button). Note, too, the vertical, rather than horizontal, appearance of a pop-up menu.
Constructing a Pop-up Menu
The code required to create a pop-up menu is actually not that different from a normal menu. Java provides a special class,
PopupMenu menu = new PopupMenu();
menu.add ( new MenuItem ("Copy") );
menu.add ( new MenuItem ("Paste") );
In this case, we add the appropriate
Using a Pop-up Menu
The trick with a pop-up menu is using it. After all, the pop-up menu must remain invisible until called upon — otherwise your user would have a really annoying menu floating across the applet or application screen! You could trigger a pop-up menu for any reason, but the one that makes the most sense, is in response to user interaction. By default, the pop-up action on a Wintel system is a click of the right mouse button. However, on other systems it may be to hold the mouse down, or some other action. So it’s best not to hardwire the activation of the pop-up for a specific platform.
Java actually makes it fairly easy to avoid this. The
So, to check for a pop-up request, we need to write a
void show ( java.awt.Component, int offsetX, int
You’ll notice that the method requires a
Putting It All Together
The best way to learn something is to see it working in real life. I’ve put together a very simple applet, that allows you to change its background color by using a pop-up menu. The applet can be seen below, along with the
Figure 3. Applet with a pop-up menu.
The applet itself is fairly simple. The menu is activated when the mouse button is released, and in response to selection of a menu item, the applet will change color and repaint itself. Though this is, of course, a trivial example, in more complex applications a pop-up menu might even be context-sensitive, changing for the type of component on which it was activated. For example, right clicking over a text area might offer a different menu to right clicking on a picture or a button.
Pop-up menus can add a professional touch to your Java software, as well as making software easier for end-users. The code required to create them is fairly small and simple, but the effect can be very impressive. Even applets can use pop-up menus, though they’re also ideal for use in standalone applications with a GUI. Consider adding a pop-up menu to your next application — both you and your users will be pleased with the result.
About the Author
David Reilly is a software engineer and freelance technical writer living in Australia. A Sun Certified Java 1.1 Programmer, his research interests include the Java programming language, networking & distributed systems, and software agents. He can be reached via e-mail at [email protected], or his personal Website.