1

My program has a menu bar with one menu one it called "File." Inside the file menu there are 4 options: "New", "Pause", "Unpause", and "Exit". All five of these have mnemonics assigned however only the one for File works the way I had hoped.

The four others work, but they only work if I activate the mnemonic for File first. i.e. To activate "New" I need to press Alt+F, the Alt+N. I didn't think mnemonics were suppose to work that way, but I could be mistaken.

This is the code I currently have, maybe someone can point out what I'm doing wrong.

   //MENU BAR
   private JMenuBar menuBar;
   private JMenu fileMenu;
   private JMenuItem newGame;
   private JMenuItem pauseGame;
   private JMenuItem unpauseGame;
   private JMenuItem exitGame;


    //CREATE THE FILE MENU
   public void buildMenuBar(){

      //INITIAILIZE
      menuBar = new JMenuBar();

      //BUILD FILE MENU
      buildFileMenu();

      //ADD TO MENU BAR
      menuBar.add(fileMenu);

      //SET
      setJMenuBar(menuBar);

   }

   public void buildFileMenu(){

      //INITIALIZE
      fileMenu = new JMenu("File");
      newGame = new JMenuItem("New");
      pauseGame = new JMenuItem("Pause");
      unpauseGame = new JMenuItem("Unpause");
      exitGame = new JMenuItem("Exit");

      //MNEMONICS
      fileMenu.setMnemonic(KeyEvent.VK_F);    
      newGame.setMnemonic(KeyEvent.VK_N);
      pauseGame.setMnemonic(KeyEvent.VK_P);
      unpauseGame.setMnemonic(KeyEvent.VK_U);
      exitGame.setMnemonic(KeyEvent.VK_X);

      //LISTENERS
      newGame.addActionListener(new MenuListener());
      exitGame.addActionListener(new MenuListener());

      //ADD TO FILEMENU
      fileMenu.add(newGame);      
      fileMenu.add(pauseGame);
      fileMenu.add(unpauseGame);
      fileMenu.add(exitGame);
   }
zero298
  • 25,467
  • 10
  • 75
  • 100
Sean
  • 35
  • 9

1 Answers1

1

So I'm going to answer my own question. I've learned that mnemonics like I was trying to use only work when the menu is active. That's why they worked for the "File" option but not the "New Game" option unless the file option was already open. It was working right, just not how I understood it to work.

Sean
  • 35
  • 9