JDK-4108937 : JMenu ignores the ActionListener event
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.5.1
  • CPU: generic
  • Submitted: 1998-02-03
  • Updated: 2000-03-23
  • Resolved: 2000-03-23
Description
I can add the ActionListener to JMenu but has no effect. The JMenu
inherits the addActionListener method from AbstractButton which means I can
register an ActionListener to JMenu itself. By looking at the code in
AbstractButton class it seems to me you are adding the ActionListerner 
objects into the wrong field which is named "listenerList" instead of
actionListener field.

import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import java.awt.event.*;
import java.awt.*;


public class MenuTest extends JFrame
{
    public static void main(String[] args)
    {
        MenuTest mt = new MenuTest();
        JMenuBar mb = new JMenuBar();
        JMenu m = new JMenu("File");

        //inner class
        m.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {   System.out.println("Actionevent " + evt);
            }
        });

        mb.add(m);
        mt.setJMenuBar(mb);
        mt.setSize(500, 500);
        mt.setVisible(true);
    }
}


nasser.nouri@Corp 1998-02-02

Comments
WORK AROUND In practice this is easy to workaround by using the MenuEvent and MenuListeners provided in Swing.
11-06-2004

EVALUATION The observation that no actionEvent is fired is correct, but the reason why is something totally different. The listenerList is the correct way for these listeners to be stored in Swing, see swing.event.EventListenerList.java The problem is that the JMenu UIs do not make the JMenu's model undergo the same transistions as the JButton/JMenuItem models, so there is never a case where the actionEvent is fired. In practice this is easy to workaround by using the MenuEvent and MenuListeners provided in Swing. georges.saab@Eng 1998-03-17
17-03-1998