驯服 Tiger: Context popup menus 作者:John Zukowski

类别:Java 点击:0 评论:0 推荐:
Taming Tiger: Context popup menus
Automated popup menu handling

John Zukowski ([email protected])
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:

Listing 1. Creating a JPopupMenu

JMenuItem aMenuItem1 = ...; JMenuItem aMenuItem2 = ...; JMenuItem aMenuItem3 = ...; JPopupMenu menu = new JPopupMenu(); menu.add(aMenuItem1); menu.add(aMenuItem2); menu.add(aMenuItem3);

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:

Listing 2. Showing a JPopupMenu in Swing 1.1/1.2

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PopupSample { public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + actionEvent.getActionCommand()); } }; JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPopupMenu popupMenu = new JPopupMenu(); // Cut JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.addActionListener(actionListener); popupMenu.add(cutMenuItem); // Copy JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.addActionListener(actionListener); popupMenu.add(copyMenuItem); // Paste JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.addActionListener(actionListener); pasteMenuItem.setEnabled(false); popupMenu.add(pasteMenuItem); // Separator popupMenu.addSeparator(); // Find JMenuItem findMenuItem = new JMenuItem("Find"); findMenuItem.addActionListener(actionListener); popupMenu.add(findMenuItem); MouseListener mouseListener = new MouseAdapter() { private void showIfPopupTrigger(MouseEvent mouseEvent) { if (mouseEvent.isPopupTrigger()) { popupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY()); } } public void mousePressed(MouseEvent mouseEvent) { showIfPopupTrigger(mouseEvent); } public void mouseReleased(MouseEvent mouseEvent) { showIfPopupTrigger(mouseEvent); } }; frame.addMouseListener (mouseListener); frame.setSize(350, 250); frame.setVisible(true); } }

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:

Listing 3. Showing a JPopupMenu in Swing 1.3

private void showIfPopupTrigger(MouseEvent mouseEvent) { if (popupMenu.isPopupTrigger(mouseEvent)) { // This line here changed popupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY()); } }

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:

Listing 4. Showing a JPopupMenu in Tiger

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PopupSample15 { public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + actionEvent.getActionCommand()); } }; JFrame frame = new JFrame("Tiger Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPopupMenu popupMenu = new JPopupMenu(); // Cut JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.addActionListener(actionListener); popupMenu.add(cutMenuItem); // Copy JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.addActionListener(actionListener); popupMenu.add(copyMenuItem); // Paste JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.addActionListener(actionListener); pasteMenuItem.setEnabled(false); popupMenu.add(pasteMenuItem); // Separator popupMenu.addSeparator(); // Find JMenuItem findMenuItem = new JMenuItem("Find"); findMenuItem.addActionListener(actionListener); popupMenu.add(findMenuItem); JButton button = new JButton("Hi"); button.setComponentPopupMenu(popupMenu); frame.getContentPane().add(button, BorderLayout.CENTER); frame.setSize(350, 250); frame.setVisible(true); } }

Figure 2. JPopupMenu in Tiger

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.

本文地址:http://com.8s8s.com/it/it15895.htm