Domanda

When using java-7 on osx, if I set the hotkey Command + Equals to a menu item, it is called more than once when I trigger it.

I wrote a simple application to demonstrate this behaviour. The hotkey just prints out the system time in the console.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class newWindow {
    static JFrame newWindow;

    public static void main(String args[]) {
        new newWindow().createWindow();
    }

    public static void createWindow() {
        newWindow = new JFrame("Window1");
        //Where the GUI is created:

        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();
        newWindow.setJMenuBar(menuBar);
        newWindow.setVisible(true);

        //Build the first menu.
        menu = new JMenu("A Menu");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("A text-only menu item");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                System.out.println(System.currentTimeMillis());
            }
        });
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS,
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            menu.add(menuItem);
    }
}

Feel free to try the code and let me know if you guys experience the same thing.

È stato utile?

Soluzione

I have the same problem. However, the behavior seems to be fixed in Java 1.8, so it seems to have been fixed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top