JPopupMenu behavior

I have prepared a small test case below. My problem is when I right click on the window. JPopupMenu appears, but if I click anywhere outside of the JWindow menu it doesn't disappear. I have to click somewhere in the window to get rid of it, which is not the expected behavior.

EDIT: After reading akf's answer, I switched to JFrame when the frame is in focus and the popup menu shows it disappears when you click on another window. but if the window has no focus and you click somewhere the menu will not disappear.


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

class test {

    static class window extends JWindow
    implements MouseListener, MouseMotionListener{

    JPopupMenu popMenu;
    JPanel panel = new JPanel();

    Point location;
    MouseEvent pressed;

    public window(){

        addMouseListener( this );
        addMouseMotionListener( this );

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

        initPopMenu();
        add(label);
        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();
    }
}

      

+2


source to share


3 answers


Take a look at the Java Doc on JWindow.isFocusableWindow A JWindow cannot be a focused window unless it has an owner and owner. You are using the default constructor, so your JWindow has a shared owner asn which is not configurable. When it is out of focus, it cannot detect the loss of focus when you click somewhere else.



I changed JWindow

to JFrame

and added a call setUndecorated(true);

before the call setVisible

and it works for me. If these changes don't get you working, please post the Java version you are using:java -fullversion

+1


source


In Java 6 on Windows, I cannot get the popup to even display with the code you provided. On the other hand, if I override your superclass in the JFrame, it works as desired with the popup disappearing when I click the button outside the window. Is there a reason you are using JWindow

as your superclass instead of JFrame

? If you want to have a borderless / no title bar, you can call setUndecorated(true)

on JFrame

(before setting visible and packed, of course.)



0


source


How can I hide the menu if it is visible from the method MouseExited

?

0


source







All Articles