I create a JFrame and place a JMenuBar into it, a "Copy" menu item with "Ctrl+C" accelerator is added. The complete source code are pasted below. When I do drag and drop within the JFrame, I can see the "Ctrl+C" accelerator is triggered (since ActionEvent is printed in console), which just like you press Ctrl+C on keyboard.
I think it is quite strange behavior and I could not figure out why the mouse manipulation will trigger that hotkey. Is it a bug?
public class Test {
public static void main(String[] args) {
final JFrame jf = new JFrame("Test");
final JMenuBar menuBar = new JMenuBar();
jf.setJMenuBar(menuBar);
final JMenu menu = new JMenu("Edit");
menuBar.add(menu);
final JMenuItem copyItem = new JMenuItem("Copy");
copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
});
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
menu.add(copyItem);
jf.setPreferredSize(new Dimension(400, 300));
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Some days ago I had a similar problem. But I've resolved it changing the
Event.CTRL_MASK
parameter forKeyEvent.CTRL_DOWN_MASK
. My final code was the following:I don't know if this is a well-known bug, but my option works in my case without any problem.
Good luck with it!