• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add a shortkey to Menu

 
Karan Galhotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends

I m new to Swings. This is my code written below: -

import javax.swing.*;
class menushortkey
{
void simplemenu()
{
JFrame frame = new JFrame();
JMenuBar menubar = new JMenuBar();
JMenu menu1 = new JMenu("my menu");
JMenuItem item1 = new JMenuItem("HIDE");
JMenuItem item2 = new JMenuItem("SHOW");
menu1.add(item1);
menu1.add(item2);
menubar.add(menu1);
frame.setJMenuBar(menubar);
frame.setVisible(true);
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
menushortkey sh = new menushortkey();
sh.simplemenu();
}
}
Now i want to open "My Menu" with the help of alt+m(Keys). Plzz help me to do this.

THkx in advance.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
menu1.setMnemonic('m');
 
Karan Galhotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx Michael
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!