PDA

View Full Version : MenuShortCuts (Java)




Azrel
Mar 27, 2006, 01:54 PM
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!!



MarkCollette
Mar 28, 2006, 10:06 PM
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:


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 );

Azrel
Mar 30, 2006, 04:38 AM
I solved it by just using Menu rather than JMenu. Thanks anyway :-)

MarkCollette
Mar 30, 2006, 12:24 PM
I solved it by just using Menu rather than JMenu. Thanks anyway :-)

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.