我有一个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);
使用getInvoker()
方法。
Component invoker = popupMenu.getInvoker();
使用event.getSource()
方法;
FWIW,
((JPopupMenu)((JMenuItem)e.getSource()).getParent()).getInvoker()
OMG ...