1 Reply Latest reply: Apr 2, 2007 7:23 AM by 807596 RSS

    Event in Menu

    807596
      the book says we can add ActionEvent to menu object(though it is not preferred)
      but when I do this in my code it does not work. It shows nothing when I click the menu.
      import java.awt.*;
                       import java.awt.event.*;
               
                       class ExamDemo{
                             public ExamDemo() {
                               Frame f=new Frame("Frame");
                               MenuBar m1=new MenuBar();
                               Menu menu1=new Menu("File");
                               menu1.addActionListener(new MyListener());
                               m1.add(menu1);
                               f.setMenuBar(m1);
                               f.setSize(200,300);
                               f.setVisible(true);
                             }
                             public static void main(String args[]){
                             
                             new ExamDemo();     
                             }
                         }
                      class MyListener implements ActionListener {
                         public void actionPerformed(ActionEvent ae) {
                            System.out.println("You have clicked me!");
                         }
                      }
        • 1. Re: Event in Menu
          807596
          For Menu/JMenu, addActionListener() is available.
          But check the following link for Listeners that Swing Components Support
          http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponents.html#many

          In that MenuListener alone is listed for menu.
          Menu selected/deselected/canceled that can be handled by MenuListener
          http://java.sun.com/javase/6/docs/api/javax/swing/event/MenuListener.html