title: Java JMenuItem ActionListener
body: <p>I need some help with my ActionListener on my JMenuBar.</p>
  
  <p>There's no error; however every time I click on a JMenuItem, it triggers all the action associated with the JMenuItem. My question is: am I doing it right in my ActionListener code? I'm not too sure about my ActionListener class. I'm trying to separate my ActionListener from my Button logic. </p>
  
  <p>If anyone has any ideas on what I may be doing wrong, please point it out.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package MenuBar;
  import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  public class SimpleMenuBar{
      private static final LayoutManager grid = new GridLayout(0, 1);
      public JMenuBar MenuBar;
      public JMenu MenuFile, MenuEdit, MenuOption;
      public JMenuItem ItemNew, ItemOpen, ItemSave, ItemExit, ItemCopy, ItemCut, ItemPaste;
      ButtonGroup direction;
      JRadioButtonMenuItem forwardradio, backwardradio;
      JCheckBoxMenuItem CheckCase;
      String input = null;
      public void Design()
      {
          JFrame frame = new JFrame("Simple Menubar");
          frame.setSize(320, 320);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
  
          MenuBar = new JMenuBar();
          frame.setJMenuBar(MenuBar);
          MenuBar.setLayout(grid);
  
          //Menus
          MenuFile = new JMenu("File");
          MenuFile.setMnemonic(KeyEvent.VK_F);
          MenuBar.add(MenuFile);
  
          //sub Menus
          ItemNew = new JMenuItem("New", KeyEvent.VK_N);
          MenuFile.add(ItemNew);
          ItemNew.setActionCommand("New");
          ItemNew.addActionListener(new MenuBarMethod());
  
          ItemOpen = new JMenuItem("Open", KeyEvent.VK_O);
          MenuFile.add(ItemOpen);
          ItemOpen.setActionCommand("Open");
          ItemNew.addActionListener(new MenuBarMethod());
          MenuFile.addSeparator();
  
  
          ItemSave = new JMenuItem("Save", KeyEvent.VK_S);
          MenuFile.add(ItemSave);
          MenuFile.addSeparator();
  
          ItemExit = new JMenuItem("Exit", KeyEvent.VK_X);
          MenuFile.add(ItemExit);
  
          MenuEdit = new JMenu("Edit");
          MenuFile.setMnemonic(KeyEvent.VK_E);
          MenuBar.add(MenuEdit);
  
          ItemCopy = new JMenuItem("Copy", KeyEvent.VK_C);
          KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke("control C");
          ItemCopy.setAccelerator(ctrlCKeyStroke);
          MenuEdit.add(ItemCopy);
  
          ItemCut = new JMenuItem("Cut", KeyEvent.VK_V);
          KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V");
          ItemCut.setAccelerator(ctrlVKeyStroke);
          MenuEdit.add(ItemCut);
  
          ItemPaste = new JMenuItem("Paste", KeyEvent.VK_Y);
          KeyStroke ctrlYKeyStroke = KeyStroke.getKeyStroke("control Y");
          ItemPaste.setAccelerator(ctrlYKeyStroke);
          ItemPaste.setEnabled(false);
          MenuEdit.add(ItemPaste);
          MenuEdit.addSeparator();
  
          MenuOption = new JMenu("Option");
          Icon atIcon = new ImageIcon("option.png");
          MenuOption.setIcon(atIcon);
          MenuOption.setMnemonic(KeyEvent.VK_O);
  
          direction = new ButtonGroup();
          forwardradio = new JRadioButtonMenuItem("Forward Me", true);
          forwardradio.setMnemonic(KeyEvent.VK_F);
          MenuOption.add(forwardradio);
          direction.add(forwardradio);
          MenuEdit.add(MenuOption);
  
          backwardradio = new JRadioButtonMenuItem("Backward Me");
          backwardradio.setMnemonic(KeyEvent.VK_B);
          MenuOption.add(backwardradio);
          direction.add(backwardradio);
  
          MenuOption.addSeparator();
  
          CheckCase = new JCheckBoxMenuItem("Case Sensitive");
          MenuOption.add(CheckCase);
          direction.add(CheckCase);
          MenuEdit.add(MenuOption);
  
      }
      public static void main(String[] args)
      {
          SwingUtilities.invokeLater(new Runnable(){
              public void run()
              {
                  SimpleMenuBar MyMenu = new SimpleMenuBar();
                  MyMenu.Design();
  
              }
          });
      }
  }
  </code></pre>
  
  <p>It's an awful lot of code for just a menu bar. I haven't fully implemented the ActionListener. For testing purposes, I've only implemented it for two items: <code>ItemNew</code> and <code>ItemOpen</code>. Unfortunately, every time I click on the <code>ItemNew</code> menu item, it triggers the actions of both <code>ItemNew</code> <em>and</em> <code>ItemOpen</code>.</p>
  
  <p>Here are is code for my action listener:</p>
  
  <pre><code>package MenuBar;
  import java.awt.event.*;
  
  import javax.swing.JOptionPane;
  public class MenuBarMethod implements ActionListener{
      public void actionPerformed(ActionEvent e)
      {
          if("New".equals(e.getActionCommand())){
              JOptionPane.showMessageDialog(null, "Selected Item: " + e.getActionCommand());
          }
          if("Open".equals(e.getActionCommand())){
              JOptionPane.showMessageDialog(null, "Selected Item: " + e.getActionCommand());
          }
      }
  
  }
  </code></pre>