简体   繁体   English

Java JPopupMenu Mac OS X.

[英]Java JPopupMenu Mac OS X

i have three problems with a JPopupmenu on Mac, all can be reproduced by the enclosed java program or eg the netbeans java application. 我在Mac上使用JPopupmenu有三个问题,所有这些都可以通过附带的java程序或netbeans java应用程序进行复制。

The first thing is that Java applications don't block the dock when a popup menu is shown. 第一件事是Java应用程序在显示弹出菜单时不会阻塞停靠。 So when i right click in my java application to open a popup menu, i can still move the mouse over the dock area and the dock appears. 因此,当我右键单击我的java应用程序打开弹出菜单时,我仍然可以将鼠标移动到停靠区域并显示停靠栏。 In non java applications (Outlook, Textwrangler, Finder...) the dock won't appear if a context menu is shown in these applications. 在非Java应用程序(Outlook,Textwrangler,Finder ...)中,如果在这些应用程序中显示上下文菜单,则不会出现停靠栏。

Is there a way to make a java application behave like a 'native' OS X application, so the dock will not be shown in this context? 有没有办法让java应用程序像'本机'OS X应用程序一样,所以Dock不会在这个上下文中显示?

The next problem is more annoying. 下一个问题更令人讨厌。 if the context menu is shown by the java application and now the user switches (cmd-TAB or by the dock) to another application lets say Outlook, the context menu of the java application is still visible on top of the other application window. 如果java应用程序显示了上下文菜单,现在用户切换(cmd-TAB或通过dock)到另一个应用程序就可以说是Outlook,java应用程序的上下文菜单仍然可以在另一个应用程序窗口的顶部看到。

Is there a way to hide the popup menu of the java application if another application has the focus? 有没有办法隐藏java应用程序的弹出菜单,如果另一个应用程序有焦点?

And the last problem. 最后一个问题。 Lets say an application is in front of netbeans and now you right click into the netbeans window, a popup menu from netbeans is shown, but if you move the mouse over the menu items, no menu item will be highlighted. 让我们说应用程序在netbeans前面,现在你右键单击netbeans窗口,显示netbeans的弹出菜单,但是如果你将鼠标移到菜单项上,则不会突出显示任何菜单项。 You're able to select a menu item by pressing the mouse, but by moving the mouse over the menu items they are not highlighted. 您可以通过按鼠标选择菜单项,但是通过将鼠标移到菜单项上,它们不会突出显示。

Why are the menu items not highlighted, is there a workaround? 为什么菜单项没有突出显示,是否有解决方法?

Mac OS X 10.6.8 Java: 1.6.0_35 Mac OS X 10.6.8 Java:1.6.0_35

package popupmenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class PopupMenuApp {
private JPopupMenu popup;

private class PopupListener extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
}

private void start() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            popup = new JPopupMenu();
            popup.add(new JMenuItem("A popup menu item"));
            frame.addMouseListener(new PopupListener());

            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setAlwaysOnTop(true);
        }
    });
}

public static void main(String[] args) {
    PopupMenuApp app = new PopupMenuApp();
    app.start();
}
}

For the second issue, adding a WindowFocusListener does the trick: 对于第二个问题,添加WindowFocusListener可以解决这个问题:

popupMenu.show(button, 0, bounds.height + 1);
frame.addWindowFocusListener(new WindowFocusListener() {
    @Override public void windowGainedFocus(WindowEvent arg0) {}
    @Override public void windowLostFocus(WindowEvent arg0) {
        popupMenu.setVisible(false);
        frame.removeWindowFocusListener(this);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM