JPopupMenu Java

1

I am working with a JPopupMenu in NetBeans, I want my JPopupMenu to disappear immediately the mouse is out of this, I tried putting a listener to the JPopupMenu of type mouseExited, but what happens is that as soon as the mouse rests on top of a JMenuItem, which is part of this the listener is executed making the window disappear. How can I solve that? Is there any other way to do this?

    
asked by Daniel Morales 21.12.2018 в 22:29
source

1 answer

0

The solution was found, creating a routine which is instantiated as soon as the JPopupMenu becomes visible, this will be constantly checking that the position of the mouse is not greater than the JPopupMenu position + the size, the routine is destroyed just the JPopupMenu disappears. I share the code in case someone may need it.

    Point coordenadasMouseNow = MouseInfo.getPointerInfo().getLocation();
    Point coordenadasJTable = jTable1.getLocationOnScreen();
    if(coordenadasMouseNow.x > coordenadasJTable.x+jTable1.getWidth() ||
        coordenadasMouseNow.x < coordenadasJTable.x ||
        coordenadasMouseNow.y > coordenadasJTable.y+jTable1.getHeight() ||
        coordenadasMouseNow.y < coordenadasJTable.y){
        jPopupMenu1.setVisible(false);
        return;
    }
    rutina = new Timer(60, (ActionEvent e) -> {
        Point coordenadasMouse = MouseInfo.getPointerInfo().getLocation();
        Point coordenadasJPopupMenu = jPopupMenu1.getLocationOnScreen();
        if(coordenadasMouse.x > coordenadasJPopupMenu.x+jPopupMenu1.getWidth() ||
           coordenadasMouse.x < coordenadasJPopupMenu.x ||
           coordenadasMouse.y > coordenadasJPopupMenu.y+jPopupMenu1.getHeight() ||
           coordenadasMouse.y < coordenadasJPopupMenu.y){
            jPopupMenu1.setVisible(false);
        }
    });
    rutina.start();
    
answered by 23.12.2018 в 04:04