我有一个JPopUpMenu,我已将其添加到多个JTables中,并且希望获得右键单击的特定表,以便可以对其进行更改。如何在 Action 监听器中获取触发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();
关于java - 如何获取调用JPopupMenu的组件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12420878/