Java Pop Up menu on Mac OS

I am trying to get the JPopUpMenu show when the user right click on my JTable. In my class that extends the JTable, I call the following code:

addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            rowClicked = rowAtPoint(e.getPoint());
            colClicked = columnAtPoint(e.getPoint());
            if (e.isPopupTrigger())
            {
                popUpMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
            rowClicked = rowAtPoint(e.getPoint());
            colClicked = columnAtPoint(e.getPoint());

            if(e.isPopupTrigger())
            {
                popUpMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

      

Whenever I right click on a track, with my mouse, or with Ctrl + right click, it if(e.isPopupTrigger())

never evaluates to true, and the menu is never shown. I have breakpoints to check.

I have done some research online and it looks like this solution should work. Since right clicks are platform dependent, using the isPopupTrigger () method should be the way to go.

Is there something special that happens when I'm on a mac?

+3


source to share


1 answer


This simple example works for me, maybe it can help you find your problem. I am on Mac using Java 7.

enter image description here



public static void main(String[] args)
{
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    String columnNames[] = { "Column 1", "Column 2", "Column 3" };

    String dataValues[][] = { { "12", "234", "67" }, { "-123", "43", "853" }, { "93", "89.2", "109" }, { "279", "9033", "3092" } };
    JTable table = new JTable(dataValues, columnNames);

    panel.add(table);

    final JPopupMenu menu = new JPopupMenu();
    JMenuItem item = new JMenuItem("item");
    menu.add(item);
    table.setComponentPopupMenu(menu);

    table.addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            if (e.isPopupTrigger())
            {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
            if (e.isPopupTrigger())
            {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

      

+4


source







All Articles