JMenuItem with ActionListener

When the user selects a menu, an action event is triggered. You need to install an action listener for each menu item.

menuItem.addActionListener(this);

Program

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

class JMenuBarTest extends JFrame implements ActionListener{
  
    private JMenuItem   New;
    private JMenuItem   Open;
    private JMenuItem   Save;
    private JMenuItem   SaveAs;
    private JMenuItem   Exit;
    private JMenuItem   Cut;
    private JMenuItem   Copy;
    private JMenuItem   Paste;
    private JTextField  jt;
  
    JMenuBarTest()
    {
        setTitle("JMenuBar_test");
        setLayout(new FlowLayout());
        setJMenuBarAndMenuBarItems();
        setAction();
        setJTextField();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
  
    void setJMenuBarAndMenuBarItems()
    {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu1    = new JMenu("File");
        New      = new JMenuItem("New");
        Open     = new JMenuItem("Open");
        Save     = new JMenuItem("Save");
        SaveAs   = new JMenuItem("SaveAs");
        Exit     = new JMenuItem("Exit");
        menu1.add(New);
        menu1.add(Open);
        menu1.addSeparator();
        menu1.add(Save);
        menu1.add(SaveAs);
        menu1.addSeparator();
        menu1.add(Exit);
        JMenu menu2    = new JMenu("Edit");
        Cut      = new JMenuItem("Cut");
        Copy     = new JMenuItem("Copy");
        Paste    = new JMenuItem("Paste");
        menu2.add(Cut);
        menu2.add(Copy);
        menu2.add(Paste);
        menuBar.add(menu1);
        menuBar.add(menu2);
        setJMenuBar(menuBar);
    }
  
    void setJTextField()
    {
        jt = new JTextField(10);
        add(jt);
    }
  
    void setAction()
    {
        New.addActionListener(this);
        Open.addActionListener(this);
        Save.addActionListener(this);
        SaveAs.addActionListener(this);
        Exit.addActionListener(this);
        Cut.addActionListener(this);
        Copy.addActionListener(this); 
        Paste.addActionListener(this);
    }
  
    public void actionPerformed(ActionEvent eve)
    {
        if(eve.getSource() == New)
            jt.setText("New");
        else if(eve.getSource() == Open)
            jt.setText("Open");
        else if(eve.getSource() == Save)
            jt.setText("Save");
        else if(eve.getSource() == SaveAs)
            jt.setText("SaveAs");
        else if(eve.getSource() == Exit)
            System.exit(0);
        else if(eve.getSource() == Cut)
            jt.setText("Cut");
        else if(eve.getSource() == Copy)
            jt.setText("Copy");
        else if(eve.getSource() == Paste)
            jt.setText("Paste");
    }
}

public class Javaapp {
  
    public static void main(String[] args) {
      
        JMenuBarTest jm = new JMenuBarTest();
    }
}


Program Output

Leave a Comment