MenuShortCuts (Java)

Discussion in 'Mac Programming' started by Azrel, Mar 27, 2006.

  1. macrumors regular

    Azrel

    Joined:
    Jun 8, 2005
    #1
    Hey guys,

    I think this is a Mac issue, but for some reason my MenuShortCut's don't seem to work:

    JMenuItem newItem = new JMenuItem("New Game \t ?N", KeyEvent.VK_A);
    newItem.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    newGame();
    }
    });
    fileMenu.add(newItem);

    Also what is the KeyCode for the Apple key? And how do I use the "four leaf clover" instead of the Apple sign? I.E, it'll have "New Game.." and then the four leaf clover with an N next to it in the menu. Thanks!!
     
  2. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #2
    I don't have my Mac in front of me, or a Java compiler, but from looking at the JavaDoc, I think you should try:

    Code:
    int keyCode = java.awt.event.KeyEvent.VK_N;  // Your code used VK_A
    int modifiers = java.awt.event.InputEvent.META_DOWN_MASK;
    javax.swing.KeyStroke ks = javax.swing.KeyStroke.getKeyStroke(
        keyCode, modifiers);
    JMenuItem newItem = new JMenuItem( "New Game" );
    newItem.setAccelerator( ks );
    
     
  3. thread starter macrumors regular

    Azrel

    Joined:
    Jun 8, 2005
    #3
    I solved it by just using Menu rather than JMenu. Thanks anyway :)
     
  4. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #4
    But are you using a JFrame or a Frame? It's best not to mix Swing and AWT components, but rather to keep things consistent, otherwise you'll see repaint issues.
     

Share This Page