JDK-4765250 : REGRESSION: Mouse click event missed after use JPopupMenu in JDK1.4
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-10-18
  • Updated: 2002-10-26
  • Resolved: 2002-10-26
Related Reports
Duplicate :  
Description

Name: sv35042			Date: 10/18/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


FULL OPERATING SYSTEM VERSION :Windows 2000


A DESCRIPTION OF THE PROBLEM :
When compile and run the application with JPopupMenu in
JDK1.4.0,
the first mouse click event is missed right after the popup
menu set to invisible. Have to mouse click again to get the
mouse click event. The same code works fine in JDK1.2.2.

REGRESSION.  Last worked in version 1.2.2

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Run the sample program provide here using jdk1.4.
2.Click "Pop" button on the top of the window.
3.The Popup Menu shows with Click option.
4.Select Click, the Popup menu hides.
5.Mouse click on "Cut" button on the bottom of the window.
6.Look at the DOS command prompt, the debug code in
actionPerformed(ActionEvent e) "Got the action" does not
showed.The mouse event is missed.
7.Click the "Cut" button again, the "Got the action" shows
in the DOS window.

For the same code when you do javac & run under JDK1.2.2,
the step 6 shows the debug code in DOS window.

EXPECTED VERSUS ACTUAL BEHAVIOR :
When click the "Cut" button right after clicking "Click" on
the popup menu, the actionPerformed() should be called.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Observer;
import java.util.Observable;

// java core classes needed
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.JPopupMenu;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.JWindow;
import javax.swing.JTextField;
import javax.swing.JFrame;

public class Bug extends JPanel
   implements ActionListener, MouseListener {

   private JPopupMenu          popup        = null;
   private JButton             cutB         = null;
   private JButton             selB         = null;
   private JLabel              clickL       = null;
   
   /**
    * Basic constructor.
    */
   public Bug(){
      super(new BorderLayout());

      
      setPreferredSize(new java.awt.Dimension(240,20));
   
      clickL = new JLabel("Click");
      clickL.addMouseListener(this);
      
      cutB = new JButton("Cut");
      cutB.addActionListener(this);
      cutB.setActionCommand("CUT");
      
      selB = new JButton("Pop");
      selB.addActionListener(this);
      selB.setActionCommand("POP");
      
      popup = new JPopupMenu();
      popup.setLayout(new BorderLayout());
      popup.setBorder(null);
      popup.setPreferredSize(new java.awt.Dimension((int)getPreferredSize
().getWidth(),
                                                    (int)getPreferredSize
().getWidth()));

       
      
      add(selB,BorderLayout.NORTH);
      add(cutB,BorderLayout.SOUTH);
      
      popup.add(clickL);
   }
   
   /* May be needed later on.*/
   public void mouseEntered(MouseEvent event){ System.out.println("Enteredt");
      popup.dispatchEvent(new MouseEvent(popup, MouseEvent.MOUSE_CLICKED, 1000,
0, 2, 2, 1, false));
   }
   public void mouseExited(MouseEvent event){System.out.println("Exited");}
   public void mousePressed(MouseEvent event){ System.out.println("Pressed");}
   public void mouseReleased(MouseEvent event){
      System.out.println("Released");
   }
   public void mouseClicked(MouseEvent event){
      System.out.println("Clicked");
      popup.setVisible(false);
   }
   
   
   /** See ActionListener comments. */
   public void actionPerformed(ActionEvent e) {
      System.out.println("Got the action.");
      if (e.getActionCommand().equals("POP")) {
            popup.show(this, 0,this.getHeight());
      } else if (e.getActionCommand().equals("CLOSE")) {
         popup.setVisible(false);
      }
   }
   
   public static void main(String args[]) {
      JPanel p = new Bug();
      JFrame frame = new JFrame("Testing...");
      frame.setBounds(300,50,400,400);
      frame.getContentPane().setLayout(new BorderLayout());
      frame.getContentPane().add(p,"Center");
      frame.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
            System.exit(0); }}
      );
      frame.setVisible(true);
      frame.show();
      
      
   }
}


---------- END SOURCE ----------

Release Regression From : 1.4
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 147215) 
======================================================================

Comments
EVALUATION This is happening because we're getting a focus lost event the moment you click on the button, reassigning to awt for further investigation. ###@###.### 2002-10-18 Name: dmR10075 Date: 10/24/2002 The bug is reproducible with Mantis b03. ###@###.### 2002-10-24 ====================================================================== Name: dmR10075 Date: 10/24/2002 So far I see the following: when I press on 'Cut' button it receives focus and suddenly loses it to 'Pop'. That somehow causes loss of action event. But this lose seems to have a source in a Swing: when Swing shows popup it requests focus on JRootPane, and after popup has been hid it doesn't restore focus back to where it was(in our test scenario - to 'Pop'). So, when we I click on 'Cut' mouse event arrives to 'Cut' button as well as to mouseGrabber which issues 'cancelPopupMenu'. 'Cut' button requests focus on itself and at the moment 'cancelPopupMenu' restores focus from JRootPane to 'Pop' which brings us to the situation described in report. So, as far as I can see from this information, it is Swing problem. I didn't see any other non-predictable side events from AWT - all events are correct. Below are two stack traces from Component.requestFocus showing two sequential requests happening when I click on 'Cut' button, first one is request for focus from the button itself, second - from MouseGrabber. Need Swing engineers to looks at this before further processing. java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1071) at java.awt.Component.requestFocusHelper(Component.java:6059) at java.awt.Component.requestFocus(Component.java:5912) at javax.swing.JComponent.requestFocus(JComponent.java:1113) at javax.swing.plaf.basic.BasicButtonListener.mousePressed(BasicButtonListener.java:230) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:221) at java.awt.Component.processMouseEvent(Component.java:5060) at java.awt.Component.processEvent(Component.java:4860) at java.awt.Container.processEvent(Container.java:1569) at java.awt.Component.dispatchEventImpl(Component.java:3578) at java.awt.Container.dispatchEventImpl(Container.java:1627) at java.awt.Component.dispatchEvent(Component.java:3440) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3480) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3192) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3125) at java.awt.Container.dispatchEventImpl(Container.java:1613) at java.awt.Window.dispatchEventImpl(Window.java:1599) at java.awt.Component.dispatchEvent(Component.java:3440) at java.awt.EventQueue.dispatchEvent(EventQueue.java:451) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137) at java.awt.EventDispatchThread.run(EventDispatchThread.java:100) java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1071) at java.awt.Component.requestFocusHelper(Component.java:6059) at java.awt.Component.requestFocus(Component.java:5912) at javax.swing.JComponent.requestFocus(JComponent.java:1113) at javax.swing.plaf.basic.BasicPopupMenuUI$MenuKeyboardHelper.removeItems(BasicPopupMenuUI.java:835) at javax.swing.plaf.basic.BasicPopupMenuUI$MenuKeyboardHelper.stateChanged(BasicPopupMenuUI.java:1009) at javax.swing.MenuSelectionManager.fireStateChanged(MenuSelectionManager.java:158) at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:86) at javax.swing.MenuSelectionManager.clearSelectedPath(MenuSelectionManager.java:107) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.cancelPopupMenu(BasicPopupMenuUI.java:286) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.mousePressed(BasicPopupMenuUI.java:426) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222) at java.awt.Component.processMouseEvent(Component.java:5060) at java.awt.Component.processEvent(Component.java:4860) at java.awt.Container.processEvent(Container.java:1569) at java.awt.Component.dispatchEventImpl(Component.java:3578) at java.awt.Container.dispatchEventImpl(Container.java:1627) at java.awt.Component.dispatchEvent(Component.java:3440) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3480) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3192) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3125) at java.awt.Container.dispatchEventImpl(Container.java:1613) at java.awt.Window.dispatchEventImpl(Window.java:1599) at java.awt.Component.dispatchEvent(Component.java:3440) at java.awt.EventQueue.dispatchEvent(EventQueue.java:451) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137) at java.awt.EventDispatchThread.run(EventDispatchThread.java:100) ###@###.### 2002-10-24 ======================================================================
24-10-2002