Automated popup menu handling
John Zukowski
(jaz@zukowski.net)
President, JZ Ventures, Inc.
12 May 2004
The JPopupMenu class has been around since the beginning of Swing time. It allows you to show context-sensitive options over a part of the screen so that common operations can be close at hand. Based on which version of the Java language you were using, there were different ways to determine if a particular mouse event was the triggering event (for instance, on some platforms the event was initiated by releasing the right mouse button while on others it was by pressing another button). Taking into account right-handed and left-handed options and the number of mouse buttons available on different platforms, the task wasn't always easy. Now, with Tiger, all that code goes away -- you simply associate a JPopupMenu with any JComponent and it shows at the appropriate time automatically.
Getting started with JPopupMenu
The JPopupMenu component is the container class for context-sensitive menus -- those menus that appear when you typically right click over something on a Microsoft Windows box. In a browser, for instance, bringing up the popup menu will typically allow you to bookmark a page or view its source.
Creating a JPopupMenu for the Java platform is relatively easy. Create a JPopupMenu, stuff some JMenuItem components in it, and you have a popup menu. Listing 1 demonstrates the process:
|
Just having a popup menu isn't sufficient, though. It's your responsibility as the programmer to show the popup menu at the appropriate time and the appropriate location. That's where the fun begins. The show() method of JPopupMenu is the appropriate method to call, but when to call the method and where to show it has evolved over the various releases of the Java platform.
In the 1.1 and 1.2 releases of Swing, the MouseEvent class offered the isPopupTrigger() method. By checking the results of this method in both the mousePressed() and mouseReleased() methods of your MouseListener, you could show a popup menu at the x and y coordinates of that event and the menu would appear where the user clicked. Listing 2 shows a complete example of this approach, with the results shown in Figure 1:
|
Figure 1. JPopupMenu in action
Going from the 1.1/1.2 releases of Swing to the 1.3 release resulted in a minor change to the code in Listing 2. Instead of checking within the MouseEvent class itself to see if it is the popup trigger, the check moved into the JPopupMenu class. Given that the MouseEvent class shouldn't really know about the JPopupMenu class, it seemed like the appropriate thing to do. Listing 3 shows the change to the showIfPopupTrigger() method shown in Listing 2:
|
The rest of the program in Listing 2 stays the same and the same screen (Figure 1) would be shown when you triggered the appropriate mouse event to show the popup menu.
Tiger makes displaying popups easy
With the Tiger release, the triggering action to show a JPopupMenu has changed yet again. Sure, the old code will still work, but there is now an even easier way. You now call the newly introduced setComponentPopupMenu() method, which associates a JPopupMenu with a JComponent, so you no longer have to add the mouse listener that calls the show() method. Listing 4 shows the sample code, with the results shown in Figure 2:
|
Notice that the only thing done here was to call the setComponentPopupMenu() method of the JButton to associate the JPopupMenu with it. No show() call was necessary. With Tiger, the triggering of the popup menu is now part of the UI definition of the component. And, if that component UI wants to have a triggering event like a key click, the changes stay focused within the UI definition, not in all the uses of that popup menu.
One more thing introduced with Tiger popup menus is the setInheritsPopupMenu() method. All the components within a panel can be made to inherit the popup menu of that panel. Now you won't have to associate the popup menu with each component.
Conclusion
If you keep in mind that JFrame is not a JComponent, you'll find it easy to work with JPopupMenu components in the new way. The new setComponentPopupMenu() method simplifies your code and really moves the detection of the triggering event to the appropriate spot. As more UIs are enabled for assistive and mobile devices without mice, you can still use popup menus, but the code to use them won't need to change.
本文围绕Java的JPopupMenu组件展开,介绍其作为上下文菜单容器类的功能。阐述了在不同Java版本中,显示JPopupMenu的方法演变,包括1.1/1.2、1.3版本的实现方式,重点提及Tiger版本使显示更简单,引入新方法简化代码,还介绍了继承方法。


1584

被折叠的 条评论
为什么被折叠?



