1

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

    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();
}
}
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
Peter
  • 123
  • 2
  • 9
  • see [link](http://stackoverflow.com/questions/1498789/jpopupmenu-behavior/6791766#6791766) – Peter Jul 23 '11 at 09:57
  • I am still locking for an answer. Nobody out there in the OSX world with the same problem? – Peter Aug 01 '11 at 06:19
  • if you got answer, please share. i am having the same problem on Mac OSX – Asghar May 15 '12 at 13:03
  • A similar problem was later reported at http://stackoverflow.com/q/14193882/531762 with a different implementation of JPopupMenus. I think this is a bug in Mac OS X's Java port and you're best served by filing a bug report for it. – Thunderforge May 10 '13 at 06:52

1 Answers1

0

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);
            }
        });
sam sha
  • 842
  • 9
  • 18
  • I do not know what is your mean, but use your test code it works fine – sam sha Jul 25 '11 at 13:10
  • 1. I run the window class from eclipse (JFrame appears) 2. I 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 – Peter Jul 25 '11 at 16:50