title: How to add ActionListener to JMenuItem?
body: <p>I'm trying to add ActionListener to JMenuItem in my java menu.</p>
  
  <p>Here is a screenshot of the menu:</p>
  
  <p><img src="https://i.stack.imgur.com/UIjmT.jpg" alt="enter image description here"></p>
  
  <p>I want to add ActionListener to "Rectangle" JMenuItem in order to show up the rectangle shape upon clicking on the "Rectangle" menu item. I tried many times to add the ActionListener but I fail every time.</p>
  
  <p>Here is my code:</p>
  
  <p>Class "menubar.java" :</p>
  
  <pre><code>import javax.swing.*;
  
  public class menubar extends JFrame{
  
  public menubar(){
      JMenuBar menubar = new JMenuBar();
      setJMenuBar(menubar);
  
      JMenu shape = new JMenu("Shape");
      menubar.add(shape);
  
      JMenuItem rect = new JMenuItem("Rectangle");
      shape.add(rect);
  
      JMenuItem star = new JMenuItem("Star");
      shape.add(star);
  
      JMenu color = new JMenu("Color");
      menubar.add(color);
  
      JMenuItem black = new JMenuItem("Black");
      color.add(black);
  
      JMenuItem orange = new JMenuItem("Orange");
      color.add(orange);
  }
  
  public static void main(String[] args) {
      menubar gui = new menubar();
      gui.setTitle("Menu Bar");
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      shapes SPS = new shapes();
      gui.add(SPS);
      gui.setSize(500,300);
      gui.setVisible(true);
      gui.setLocationRelativeTo(null);
  }
  }
  </code></pre>
  
  <p>Class "shapes.java" :</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  
  import javax.swing.*;
  
  public class shapes extends JPanel{
  int midX = 220;
  int midY = 90;
  int radius[] = {60,20,50,20};
  int nPoints = 16;
  int[] X = new int[nPoints];
  int[] Y = new int[nPoints];
  
  public void paintComponent(Graphics gphcs){
  super.paintComponent(gphcs);
  this.setBackground(Color.WHITE);
  
  gphcs.setColor(Color.BLUE);
  gphcs.fillRect(20,35,100,30);
  
  gphcs.setColor(Color.RED);
  gphcs.drawString("Welcome to Java", 20, 20);
  
  for (int i=0; i &lt; nPoints; i++) {
         double x = Math.cos(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];
         double y = Math.sin(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];
  
         X[i] = (int) x + midX;
         Y[i] = (int) y + midY;
  }
  gphcs.setColor(Color.GREEN);
  gphcs.fillPolygon(X, Y, nPoints);
  }
  }
  </code></pre>
  
  <p>I would be very thankful if anybody helped me with this issue.</p>
  
  <p>Thanks for your time..</p>