开发者

JPopupMenu Behavior OSX 10.6.7

My problem is when I right click on the JFrame in the example below, the JPopupMenu shows up but if I click anywhere outside the JFrame, the menu does not disappear. I have to click somewhere on the JFrame to get rid of it which is not the expected behavior. Here are the steps to reproduce:

  1. Run the window class from Eclipse (JFrame appears)
  2. Click into the Eclipse workspace (JFrame loses focus and is hidden behind eclipse)
  3. Minimize Eclipse (JFrame appears)
  4. Go with your mouse over JFrame and make a right click (Popup appears)
  5. Click somewhere (Not into JFrame or Popup). The Popup will not disappear

I'm running OS X 10.6.7 and Java full version 1.6.0_24-b07-334

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class test
{

static class window extends JFrame implements MouseListener,
        MouseMotionListener
{

    JPopupMenu popMenu;
    JPanel panel = new JPanel();

    Point location;
    MouseEvent pressed;

    public window()
    {

        addMouseListener(this);
        addMouseMotionListener(this);

        JLabel label = new JLabel("JFrame", JLabel.CENTER);

        initPopMenu();
        add(label);
        setUndecorated(true);
        setVisible(true);

        // setAlwaysOnTop(true);
        setLocationRelativeTo(null);
        pack();
    }

    public void initPopMenu()
    {
        popMenu = new JPopupMenu();
        JMenuItem item;

        item = new JMenuItem("Title");
        item.setEnabled(false);
        popMenu.add(item);
        popMenu.addSeparator();

        item = new JMenuItem("Item One");
        popMenu.add(item);

        item = new JMenuItem("Item 2");
        popMenu.add(item);

        item = new JMenuItem("Item 3");
        popMenu.add(item);
    }

    public void mousePressed(MouseEvent e)
    {
        pressed = e;
        int nModifier = e.getModifiers();
        if (((nModifier & InputEvent.BUTTON2_MASK) != 0)
                || ((nModifier & InputEvent.BUTTON3_MASK) != 0))
            popMenu.show( this, e.getX(), e.getY() );
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }
开发者_C百科
    public void mouseDragged(MouseEvent me)
    {
    }

    public void mouseMoved(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }
}

public static void main(String[] args)
{
    window dw = new window();
}
}


you can add a windowFocusListener, hide the menu when window lost focus

        this.addWindowFocusListener(new WindowFocusListener() {
            @Override
            public void windowLostFocus(WindowEvent e) {
                if(popMenu != null){
                    popMenu.setVisible(false);
                }
            }
            @Override
            public void windowGainedFocus(WindowEvent e) {
                //System.out.println(e);
            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜