分享
 
 
 

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

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Taming Tiger: Context popup menus

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:

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有