Adding an ActionListener to JMenuItem

29,043

What it looks to me is you have

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");

outside of any method definition and there is no way to call that code.

Try this:

public class Main extends JFrame{

  //initialize integer height/width values along with declaring 
  //Swing component variables
  private final int W = 500,
                    H = 500;

  private JMenu file, headMenu, bgMenu;
  private JMenuBar menuBar;
  private JMenuItem exitItem;

  //constructor
  public Main(){
    setTitle("Move the Head");
    setSize(W, H);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    initializeElements();

  }

  //Initializes the elements, this part is missing from your code above.
  public void initializeElements(){

    menuBar = new JMenuBar();
    file = new JMenu("File");
    exitItem = new JMenuItem("Exit");

    exitItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        System.exit(0);
      }
    });

    headMenu = new JMenu("Heads");
    bgMenu = new JMenu("Backgrounds");

  }

  public static void main( String[] args ) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Main f = new Main();
            f.setVisible(true);
        }
    });
  }
}
Share:
29,043
liamslagle
Author by

liamslagle

Updated on July 06, 2022

Comments

  • liamslagle
    liamslagle almost 2 years

    I've just begun a simple GUI project, and while creating the menu bar, I ran into an error that I find to be inexplicable. I want to add an ActionListener to a JMenuItem using addActionListener, as I have done in the past. However, when I apply said method, Eclipse gives an error: "Syntax error on token "addActionListener", = expected after this token." My only thoughts are that perhaps addActionListener is being interpreted as a property rather than a method... but I have used this method in the past so I do know that it works. I am unsure of how much code I should provide, so please let me know if I should edit in more.

    package com.movethehead;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    @SuppressWarnings("serial")
    public class Main extends JFrame {
    
        private final int W = 500;
        private final int H = 500;
    
        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");
        JMenu bgMenu = new JMenu("Backgrounds"); 
    
        public Main() {
            setTitle("Move the Head");
            setSize(W, H);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
    
            add(new Pnl());
            setJMenuBar(menuBar);
        } // end constructor
    
        public static void main( String[] args ) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Main f = new Main();
                    f.setVisible(true);
            }
        });
    } // end main()
    } // end Main
    
    • Sage
      Sage over 10 years
      just post a SSCCE a compilable, runnable and able to reproduce your issue
  • liamslagle
    liamslagle over 10 years
    Ive added all of the code in the class to the question, if that helps.