MouseEvent problem in JPopupMenu

From: Chris Smith (cdsmith_at_twu.net)
Date: 01/27/05


Date: Thu, 27 Jan 2005 00:09:34 -0700

I'm having trouble getting a MouseEvent to get fired for mouseReleased
to fire in any component added to a submenu of a JPopupMenu in Java 1.5.
The same code works fine in 1.4.

Here's some simple code to demonstrate the problem. When clicking the
final button, it stays down and "released" is never printed by the
listener. Under 1.4, the button is fully operational, and "released" is
printed. The other two events (clicked and pressed) work fine in either
version.

Any ideas what's going on here? Any suggested work-arounds? The real
application where I observed this has a rather complex custom component
placed in a submenu of a JPopupMenu, and mouseReleased is needed for it
to work properly.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;

public class BugTest extends JFrame
{
    private MouseListener ml = new MouseListener() {
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited(MouseEvent e) { }

        public void mouseClicked(MouseEvent e)
        {
            System.out.println("clicked");
        }

        public void mousePressed(MouseEvent e)
        {
            System.out.println("pressed");
        }

        public void mouseReleased(MouseEvent e)
        {
            System.out.println("released");
        }
    };

    public BugTest()
    {
        super("Test Popup mouseReleased");

        JButton startButton = new JButton("Start");

        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                JPopupMenu popup = new JPopupMenu();
                JMenu middleMenu = new JMenu("Middle");
                JButton endButton = new JButton("End");

                endButton.addMouseListener(ml);

                middleMenu.add(endButton);
                popup.add(middleMenu);
                popup.show(BugTest.this, 20, 20);
            }
        });

        getContentPane().add(startButton);
        pack();
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new BugTest();
    }
}

-- 
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation