How to get the component that invoked a JPopupMenu?
Asked Answered
M

3

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);
Mcmaster answered 14/9, 2012 at 8:39 Comment(3)
+1 for using componentPopupMenu. Now use Action instead of ActionListener and it would be perfect :-)Spandau
@Spandau what do you mean? how can I use Action?Mcmaster
fastest way to learn that is to read up the tutorial (referenced in the swing tag wiki) chapters on how to use Actions/Menus :-)Spandau
A
10

Use the getInvoker() method.

Component invoker = popupMenu.getInvoker();
Abscise answered 14/9, 2012 at 8:43 Comment(3)
ps you'll need to case the ActionEvent source to a JPopupMenu reference first ;)Sellers
One problem though...the header stays selected until it's clicked on again. Any ideas?Mcmaster
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.Became
C
1

FWIW,

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

OMG...

Colossian answered 11/3, 2020 at 23:5 Comment(4)
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.Versus
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.Colossian
@Colossian I used your hints :), thanks, octal 10 years later :)Margarite
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!Derte
E
0

Use the event.getSource() method;

Effable answered 14/9, 2012 at 8:50 Comment(1)
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...Colossian

© 2022 - 2024 — McMap. All rights reserved.