Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JMenuItem ActionListener

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

}
like image 600
Zyrax Avatar asked Aug 16 '12 03:08

Zyrax


People also ask

What is the ActionListener in Java?

Interface ActionListenerThe listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method.

How do you add an action listener to JMenuItem in Java?

JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu("File"); JMenuItem exitItem = new JMenuItem("Exit"); exitItem. addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { System. exit(0); } }); JMenu headMenu = new JMenu("Heads");

What is JMenuItem in Java?

public class JMenuItem extends AbstractButton implements Accessible, MenuElement. An implementation of an item in a menu. A menu item is essentially a button sitting in a list. When the user selects the "button", the action associated with the menu item is performed.


2 Answers

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

like image 96
MadProgrammer Avatar answered Sep 18 '22 04:09

MadProgrammer


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());
        }
    }
}
like image 44
charlieBrown Avatar answered Sep 22 '22 04:09

charlieBrown