8

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);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
Igor
  • 1,532
  • 4
  • 23
  • 44

3 Answers3

10

Use the getInvoker() method.

Component invoker = popupMenu.getInvoker();
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • ps you'll need to case the `ActionEvent` source to a `JPopupMenu` reference first ;) – MadProgrammer Sep 14 '12 at 08:53
  • One problem though...the header stays selected until it's clicked on again. Any ideas? – Igor Sep 14 '12 at 10:02
  • 1
    While this answer is correct, and answers the question, it requires that you pass a JPopupMenu reference to your Action. As an alternative you could just as well pass your source reference to the Action, and the problem disappears. In many cases this could be the simpler solution. – lbalazscs Jul 07 '15 at 17:57
1

FWIW,

// ActionEvent e
((JPopupMenu)((JMenuItem)e.getSource()).getParent()).getInvoker()

OMG...

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • 5
    This is a code only answer to a ten year old question with six existing answers. When answering older questions with existing answers it is important to explain what new aspect of the question your answer addresses, and to note if the passage of time has changed the possible answers. Code only answers can almost always be improved by adding some explanation of how and why. – Andreas Mar 12 '20 at 01:40
  • This answer solves the original problem: given an ActionEvent, get the invoker of the menu. Other answers do not explain how to do this, although they do contain useful hints. But it's just *hints*, not a solution. Therefore I undeleted this answer. And as to 10 years, well, it's an octal 10, and the involved classes are still there in Java 13 and are not even deprecated. – 18446744073709551615 Mar 12 '20 at 21:20
  • @18446744073709551615 I used your hints :), thanks, octal 10 years later :) – Furkan Jul 22 '21 at 17:29
  • This is perfect! I've been banging my head against the wall trying to figure out how to find the point and then what component might map to that point, but this is a one-liner that gets exactly the required information from the ActionEvent. Thank you! – Jeremy Brooks Mar 10 '23 at 05:30
0

Use the event.getSource() method;

  • It returns a JMenuItem which is the same for both possible sources; its getParent() gives a JPopupMenu which is also the same, and its parent is null. It _is_ possible to have two or more identical menus, but... – 18446744073709551615 Mar 11 '20 at 22:52