Java JMenuItem ActionListener
Asked Answered
B

2

7

I need some help with my ActionListener on my JMenuBar.

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.

If anyone has any ideas on what I may be doing wrong, please point it out.

Here is my 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();

            }
        });
    }
}

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: ItemNew and ItemOpen. Unfortunately, every time I click on the ItemNew menu item, it triggers the actions of both ItemNew and ItemOpen.

Here are is code for my action listener:

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

}
Builtin answered 16/8, 2012 at 3:1 Comment(5)
Just as a matter of style you should camel case your variables and methods (menuFile, itemSave, itemOpen, ). Classes are Pascal case (MenuBarMethod).Gross
i'm newbie to java, anyway thank for the naming convention.Builtin
So when you click Open or New does a message dialog popup? Another thing, since your MenuBarMethod action listener handles all the actions (at least these two) you done't need to create a new instance for each menu item. Create one instance and add it to the menuitems. ActionListener l = new MenubarMethod(); openMenuItem.addActionListerner(l); newMenuItem.addActionListener(l);Gross
yes popup will show, by the way thanks a lot it's working now, i tried your code, i see my error i only need to instantiate the class actionlistener once, and call it to my menus. thanksBuiltin
it's better if you provide a picture of the JMenu after executing your codeBizarre
P
11

As a matter of personal preference, I prefer to use the Action API

The main reasons are

  • It centralise logic associated with a particular action, including name, key stroke, short cuts, icons etc
  • It's reusable. It's much easier to create an Action Object then it's to have recreate the menu item & the tool bar item & the popup menu item
  • It's well supported by other Swing components. Most Swing components that support the ActionListener API generally provide the means to supply an Action as well (JMenu.add(Action), JMenu(Action) etc)
  • It makes your code easier to read, much easier to read a single ActionPerformed method of an Action class then try and skim through multiple if statements

Take a look at How to Use Actions

IMHO

Perceivable answered 16/8, 2012 at 3:25 Comment(1)
I typically use this as well as it tends to be more reusable. Especially for menuitems used a lot such as "Save", "Save As", "Open", "Print" etc. +1Gross
B
3

I would use the getSource method of ActionEvent instead of getActionCommand.

public class MenuBarMethod implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == ItemNew){
            JOptionPane.showMessageDialog(null, "Selected Item: " + e.getActionCommand());
        }
        if(e.getSource() == ItemOpen){
            JOptionPane.showMessageDialog(null, "Selected Item: " + e.getActionCommand());
        }
    }
}
Bumkin answered 18/5, 2013 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.