javaswingjtablejpopupmenu

JPopupMenu Menu Not Appearing


This is the code for my JPopupMenu and how I added it, it is supposed to respond when I right click the table:

    JMenuItem deleteRows = new JMenuItem("Delete Row");
    popup.add(deleteRows);

    personTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON3) {
                popup.show(personTable, e.getX(), e.getY());
            }
        }

    });

I am not sure why the popup menu is not appearing when I right-click the table in the application. I would appreciate it if somebody told me what I am doing wrong.


Solution

  • The trigger for a popup is different for different OS's, you can't simply use mousePressed and your certainly shouldn't be using e.getButton() == MouseEvent.BUTTON3

    From How to use Menus, Bringing up a PopupMenu

    The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled. In the Java look and feel, the customary trigger is either pressing the right mouse button (for a popup that goes away when the button is released) or clicking it (for a popup that stays up).

    Instead, you should be checking for each of the mouse events, pressed, released and clicked. You should also be using MouseEvent#isPopupTrigger to determine if the event is a popup trigger for the OS.

    Having said all that, it would be simpler to just us JComponent#setComponentPopupMenu and let it decide instead

    personTable.setComponentPopupMenu(popup);
    

    Runnable example...

    Popup

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.table.DefaultTableModel;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    DefaultTableModel model = new DefaultTableModel(10, 10);
                    JTable table = new JTable(model);
    
                    JMenuItem mi = new JMenuItem("I'll be your menu for today");
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(mi);
    
                    table.setComponentPopupMenu(popup);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new JScrollPane(table));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }