Reputation: 1592
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);
Upvotes: 8
Views: 3113
Reputation: 16832
FWIW,
// ActionEvent e
((JPopupMenu)((JMenuItem)e.getSource()).getParent()).getInvoker()
OMG...
Upvotes: 2
Reputation: 32391
Use the getInvoker()
method.
Component invoker = popupMenu.getInvoker();
Upvotes: 10