JDK-6611524 : REGRESSION: JPopupMenu state confused MouseDragEvent
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u2
  • Priority: P2
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-10-01
  • Updated: 2011-01-19
  • Resolved: 2008-11-17
Related Reports
Relates :  
Relates :  
Description
DESCRIPTION:
------------

JPopupMenu can get confused by mouse drag events when the menu is displayed under the current mouse location.  This can cause future displays of the menu to automatically select the first menu item on the mouse release event involved with displaying the menu.

The problem is not seen (i.e. the testcase passes) with SUN JDK 1.5.0_12, 1.6.0, and 1.6.0_01. The problem was introduced in 1.6.0_02.

Considering the fixes that went into this release, it looks like 6515446 may be responsible for this regression. 

RECREATION INSTRUCTIONS
-----------------------

1. Click and release the mouse button (do not move the mouse while
   doing this!) to display the popup menu and ensure things are working
   properly. Select any item in the menu or click outside the menu area
   to make the menu go away.                                                 

2. Click and hold your mouse button to display the menu and drag the
   mouse horizontally across the first menu item a short distance. Do
   not let the mouse pointer leave the menu or the first menu item.
   Release the mouse button. This causes the problem.                             

3. Click and release the mouse button to display the popup menu again.
   The problem has now occurred. You will see the menu displayed and
   then the release of the mouse button causes the first item to be
   selected and the menu to close. This is not correct. The menu should
   stay displayed and the user should have to click again to select an
   item from the menu, even if the item they want to select is the
   first item in the menu.                                                                  

4. To reset the menu so it works properly again, click and hold the
   mouse button to display the menu and then select an item other than
   the top item.  Release the mouse button and now the menu will start
   to function normally again.


TESTCASE SOURCE
---------------

import java.awt.*;                                         
import java.awt.event.*;                                               
import javax.swing.*;                                                   
import javax.swing.border.*;                                            
import javax.swing.event.*;                                             
import java.beans.*;                                                    

                                                                        

public class PopupMenuExample extends JPanel {                          
    public JPopupMenu popup;                                            
    public PopupMenuExample() {                                         
    popup = new JPopupMenu();                                       
    ActionListener menuListener = new ActionListener() {            
    public void actionPerformed(ActionEvent event) {            
    System.out.println("Popup menu item [" +                
                                  event.getActionCommand() + "] was    
pressed.");                                                             

            }                                                           

        };                                                              

        JMenuItem item;                                                 
        popup.add(item = new JMenuItem("Left", new                      
ImageIcon("left.gif")));                                                
        item.setHorizontalTextPosition(JMenuItem.RIGHT);                
        item.addActionListener(menuListener);                           
        popup.add(item = new JMenuItem("Center",                        
                                             new                        
ImageIcon("center.gif")));                                              
        item.setHorizontalTextPosition(JMenuItem.RIGHT);                
        item.addActionListener(menuListener);                           
        popup.add(item = new JMenuItem("Right", new                     
                                                ImageIcon("right.gif")));                                               
        item.setHorizontalTextPosition(JMenuItem.RIGHT);                
        item.addActionListener(menuListener);                           
        popup.add(item = new JMenuItem("Full", new                      
ImageIcon("full.gif")));                                                
        item.setHorizontalTextPosition(JMenuItem.RIGHT);                
        item.addActionListener(menuListener);                           
        popup.addSeparator();                                           
        popup.add(item = new JMenuItem("Settings . . ."));              
        item.addActionListener(menuListener);                           
        popup.setLabel("Justification");                
        popup.setBorder(new BevelBorder(BevelBorder.RAISED));           
        popup.addPopupMenuListener(new PopupPrintListener());           
        addMouseListener(new MousePopupListener());                     
    }                                                                   
                                                          

    // An inner class to check whether mouse events are the popup       

trigger                                                                 
    class MousePopupListener extends MouseAdapter {                     
        public void mousePressed(MouseEvent e) { checkPopup(e); }       
        public void mouseClicked(MouseEvent e) {  }                     
        public void mouseReleased(MouseEvent e) { System.out.println(   
"mouseReleased" ); }                                                    
                                                                        

        private void checkPopup(MouseEvent e) {                         
            if (true||e.isPopupTrigger()) {                             
                popup.show(PopupMenuExample.this, e.getX() - 50,        
e.getY()-10);                                                           
            }                                                           
        }                                                               
    }                                                                   

                                                                        

    // An inner class to show when popup events occur                   
    class PopupPrintListener implements PopupMenuListener {             
       public void popupMenuWillBecomeVisible(PopupMenuEvent e) {      
            System.out.println("Popup menu will be visible!");          
        }                                                               

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {    
            System.out.println("Popup menu will be invisible!");        
        }                                                               

        public void popupMenuCanceled(PopupMenuEvent e) {               
            System.out.println("Popup menu is hidden!");                
        }                                                               
    }                                                                   

                                                                        

    public static void main(String s[]) {                               
        JFrame frame = new JFrame("Popup Menu Example");                
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
        frame.setContentPane(new PopupMenuExample());                   
       frame.setSize(300, 300);                                        
       frame.setVisible(true);                                         

    }                                                                   

}

Comments
EVALUATION After the fix for the 6217905 this problem is gone, closed as not reproducible
17-11-2008