如何获取调用JPopupMenu的组件?

问题描述 投票:7回答:3

我有一个JPopUpMenu,我已将其添加到多个JTables中,我想右键单击特定的表,以便对其进行更改。如何在动作侦听器中获取触发JPopupMenu的组件?

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);
java swing jtable jpopupmenu
3个回答
9
投票

使用getInvoker()方法。

Component invoker = popupMenu.getInvoker();

0
投票

使用event.getSource()方法;


0
投票

FWIW,

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

OMG ...

© www.soinside.com 2019 - 2024. All rights reserved.