简体   繁体   中英

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.

The first thing is that Java applications don't block the dock when a popup menu is shown. 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. In non java applications (Outlook, Textwrangler, Finder...) the dock won't appear if a context menu is shown in these applications.

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?

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.

Is there a way to hide the popup menu of the java application if another application has the focus?

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. 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

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:

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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