'JPopupMenu appears but seems to not react

I've problems to get a JPopupMenu working correctly.

What I expect

The menu should pop up once I do a right click with my mouse. Then I can select an item from the menu and do whatever I want..

What I actually get

The menu appears once I do a right click but after that I can not select a menu item or at least I am missing the well known mouse hover highlight effect (I would expect that the item I am currently hovering is highlighted, like it is the case in the normal menu).

The see problem here (no highlight on hover):

enter image description here

Here is my example code:

package com.mycompany.mavenproject2;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class PopupMenuTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        JPopupMenu popup = new JPopupMenu();
        
        popup.add(new JMenuItem("A"));
        popup.add(new JMenuItem("B"));
        
        pane.setSize(300,300);
        pane.add(popup);
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e)) {
                    popup.setLocation(e.getXOnScreen(), e.getYOnScreen());
                    popup.setVisible(true);
                }
            }
         });
        
        frame.setTitle("Test");
        frame.add(pane);
        frame.setPreferredSize(new Dimension(300,300));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Edit

Compare to a "normal" menu with working hovering:

enter image description here

Edit #2

Please see the current (unexpected) behaviour:



Solution 1:[1]

You need to use the JPopupMenu show method, rather than the setVisible method.

Here's the code I tested with. I'm running Windows 10 and using the Java JDK 13.0.2 with Java 8 compliance. I get the highlight on the mouse over.

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class JPopupMenuTest implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JPopupMenuTest());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();

        JPanel pane = new JPanel();
        pane.setPreferredSize(new Dimension(300, 300));
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(new JMenuItem("A"));
                    popup.add(new JMenuItem("B"));

                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        frame.setTitle("JPopupMenu Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(pane);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gilbert Le Blanc