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());
}
}
}
As a matter of personal preference, I prefer to use the Action API
The main reasons are
Action
Object then it's to have recreate the menu item & the tool bar item & the popup menu itemActionListener
API generally provide the means to supply an Action
as well (JMenu.add(Action)
, JMenu(Action)
etc)ActionPerformed
method of an Action
class then try and skim through multiple if
statementsTake a look at How to Use Actions
IMHO