4

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);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Xavier Young
  • 368
  • 3
  • 8
  • 3
    Is this working with the code you've posted? Just asking because the last time I tried drag-and-drop in Java it required quite a bit of code using a `TransferHandler`. – James P. Jul 18 '11 at 02:19
  • I second James statement. I am not able to reproduce your problem with the posted code above, perhaps because it doesn't allow drag-and-drop. – Hovercraft Full Of Eels Jul 18 '11 at 02:24
  • I am using Windows XP system, and this problem can be reproduced under JRE 1.5 or 1.6 (I didn't try 1.4). – Xavier Young Jul 18 '11 at 02:41
  • You should be able to reproduce the problem with just the code I post above, you just need to press left mouse button, move it a little bit, then release the mouse button in the JFrame area, you will see what happen in the console. – Xavier Young Jul 18 '11 at 02:43
  • I use JDK6_07 on XP and I can't duplicate the behaviour you describe. I have no idea why it would happen on any OS of Java version. – camickr Jul 18 '11 at 03:09
  • 1
    Thank you guys for the replies. I just confirmed it is caused by the dictionary tool I am using. I guess it is implemented as a mouse gesture. So it is not a programing question actually. – Xavier Young Jul 18 '11 at 03:19

1 Answers1

1

Some days ago I had a similar problem. But I've resolved it changing the Event.CTRL_MASK parameter for KeyEvent.CTRL_DOWN_MASK. My final code was the following:

   sousMenu = new JMenuItem("Nouveau", KeyEvent.VK_N);
   sousMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
                                         KeyEvent.CTRL_DOWN_MASK));
   sousMenu.setName(Modele.ID_NOUVEAU);
   sousMenu.addActionListener(this);
   menu.add(sousMenu);
   /** Modele is a singleton class with my constants
    *  My frame class implements ActionListener
    *  The KeyEvent.VK_N parameter in the constructor sets the mnemonic
    */

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!

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Charliemops
  • 749
  • 12
  • 30