我有一个添加到多个 JTable 的 JPopUpMenu,我想获得右键单击的特定表,以便对其进行更改。如何在 Action Listener 中获取触发 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);