javaswingjtablejpopupmenu

How to get the component that invoked a JPopupMenu?


I have a JPopUpMenu that I added to multiple JTables and I would like to get the specific table that's right clicked so I can make changes to it. How can I get the component that triggers the JPopupMenu in the Action Listener?

JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItemRename = new JMenuItem("Rename");
popupMenu.add(menuItemRename);
table.getTableHeader().setComponentPopupMenu(popupMenu);

ActionListener menuListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
           String newTitle = JOptionPane.showInputDialog(null, "Enter new title");
                   //Get the table and rename it here 
                }
            };
menuItemRename.addActionListener(menuListener);

Solution

  • Use the getInvoker() method.

    Component invoker = popupMenu.getInvoker();