JavaEnterprise JavaAdding Pop-up Menus to Your GUIs

Adding Pop-up Menus to Your GUIs content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

. Nothing is new so far.

Figure 1. A standard menu bar located at the top of a frame or window.

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.

Figure 2. The pop-up menu. Note the lack of a menu bar, as well as a vertical rather than horizontal appearance.

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,

, which subclasses

. So if you’re able to create menu items for a standard menu, there’s actually no difference between it and a pop-up menu. The main difference is in how they are used, not in their construction. For example, to create a simple copy-paste-popup menu, you could do the following:

PopupMenu menu = new PopupMenu();
menu.add ( new MenuItem ("Copy") );
menu.add ( new MenuItem ("Paste") );

In this case, we add the appropriate

objects to our menu container. While we want to create a pop-up menu, you could just as easily substitute the

for a

. Note, however, that you should always duplicate a pop-up menu, and should not add it to a

. If you did, you’d run into problems later. Your

would work fine, but the

wouldn’t pop-up when you needed it to. Of course, there is no good reason why you can’t share

objects, just not menus.

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

object, used by Java to provide notification of a mouse operation, offers a method to determine if a request to launch a pop-up menu is made by the user:

boolean MouseEvent.isPopupTrigger()

So, to check for a pop-up request, we need to write a

, that will check whether a pop-up menu should be triggered. Once it is detected, the final step is to activate the menu, and make it visible. Note that once a menu item has been selected, the pop-up menu will disappear; there is no need to manually hide the menu. To make the menu visible, the following method of

is used:

void show ( java.awt.Component, int offsetX, int

You’ll notice that the method requires a

as an argument. This is important, because the

will be displayed relative to a component, such as an applet or 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
source code.

This applet requires Java.
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, or his personal Website.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories