-1

I'm trying to add ActionListener to JMenuItem in my java menu.

Here is a screenshot of the menu:

enter image description here

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.

Here is my code:

Class "menubar.java" :

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

Class "shapes.java" :

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

I would be very thankful if anybody helped me with this issue.

Thanks for your time..

Mina Hafzalla
  • 2,681
  • 9
  • 30
  • 44
  • 1
    I don't see where you've tried to call `addActionListener(...)` to anything. Have you looked at the Swing menu tutorial? It's all spelled out for you there, and so there's really no need for us to regurgitate the information here since it's there for the asking. Google will help you find it -- or I can as well: [How to use Menus](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) – Hovercraft Full Of Eels May 02 '13 at 03:13
  • 2
    Seriously? Did you look at [How to use menus](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) which was linked in your previous question? Vote to close – MadProgrammer May 02 '13 at 03:20
  • 1
    @MadProgrammer: I know, it surprised me too, and I didn't know that he'd already been given the link! To the original poster, if you had already looked at the tutorial before and it confused you, then tell us exactly what confuses you in your question, but please show us that you at least try to solve it first before dumping your problem here without showing a evidence of effort. Please show us that you're not trying to be lazy about this and have others solve your problems for you. – Hovercraft Full Of Eels May 02 '13 at 03:21

1 Answers1

-1

One way that you can add an ActionListener to your container is :

public class JMenuClass extends JFrame implements ActionListener

Then, for each JMenuItem that you need a listener, you would do

item.addActionListener(this)

At the bottom of your class, you would need to add your actionPerformed methods, or however you want to implement it.

Cheers

sparkyShorts
  • 630
  • 9
  • 28
  • 5
    -1, not a good approach. First of all there is no good reason to extend a JFrame since you are not adding any new fucntionality to the frame. Also, each menu item should have its own ActionListener. – camickr May 02 '13 at 03:40
  • Yes, I was using the code above and suggesting a method that went with their code; and did I not say that each JMenuItem needs their own listener? – sparkyShorts May 02 '13 at 03:47
  • How is each JMenuItem to get a unique listener if you pass in `this`? as the ActionListener? – Hovercraft Full Of Eels May 02 '13 at 03:54
  • 1
    JMenuItem one, two, three, four; public class Example implements ActionListener //any necessary implementation one = new JMenuItem("Number One"); one.addActionListener(this); two = new JMenuItem("Number Two"); two.addActionListener(this); //and so on – sparkyShorts May 02 '13 at 04:03
  • Or I mean, the user could create class to handle the Action Event and just pass the handler into addActionListener; either way works. I have just found that for smaller programs, implementing the listener in the class definition is simple enough. – sparkyShorts May 02 '13 at 04:12