Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 326
Igor Avatar asked Sep 14 '12 08:09

Igor


1 Answers

Use the getInvoker() method.

Component invoker = popupMenu.getInvoker();
like image 168
Dan D. Avatar answered Oct 23 '22 20:10

Dan D.