2 Replies Latest reply: Feb 27, 2013 7:58 AM by 805941 RSS

    JMenuItem not receiving mouseReleased event when outside of JFrame

    805941
      Hi. It looks to me as if JMenuItems were not working as expected in large menus where some JMenuItems are outside of the JFrame that carries the menu bar.

      Such JMenuItems do trigger when being clicked. However, I sometime choose menu items by pressing the mouse botton when the cursor is over the menu title, then dragging to the menu item and releasing the mouse. This seems to only work for menu items that are inside of the JFrame.

      The provided sample code below reproduces the problem. Steps to reproduce:
      1) Compile and run below code
      2) Move mouse over the "test" menu
      3) Press left button, do not release it
      4) Move the cursor to a menu item which is inside of the JFrame while keeping the button pressed
      5) Release the mouse button -> The menu item's name is printed to stdout. -> This is the expected behavior

      ---- or

      4) Move the cursor to a menu item which is well outside of the JFrame while keeping the button pressed
      5) Release the mouse button -> The menu item's name is not printed to stdout. -> This is the unexpected behavior


      import java.awt.Dimension;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class Main 
      extends JFrame
      {
           
           private JMenuBar mainMenuBar = new JMenuBar();
           
           public Main() 
           {
                super();
                this.setJMenuBar(this.mainMenuBar);
                
                JMenu jMenu = new JMenu("test");
                ActionListener cmd = new ActionListener() {
                     
                     @Override
                     public void actionPerformed(ActionEvent arg0) {
                          System.out.println(arg0.getActionCommand());
                          
                     }
                };
                for(int i=0; i<50; i++)
                {
                     JMenuItem  jmi  = new JMenuItem("item"+i);
                     jmi.addActionListener(cmd);
                     jMenu.add(jmi);
                }
                
                this.mainMenuBar.add(jMenu);
                Dimension screenSize = getToolkit().getScreenSize();
                setLocation((int)(0.25*screenSize.width), (int)(0.25*screenSize.height));
                setSize((int)(0.5*screenSize.width), (int)(0.5*screenSize.height));
                setVisible(true);
      
           }
      
           public static void main(String[] args)
           {          
                new Main();
           }
      
      }
      JVM:
      java version "1.6.0_27"
      OpenJDK Runtime Environment (IcedTea6 1.12.3) (6b27-1.12.3-1)
      OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

      OS: Linux Aptosid (Debian sid -based)

      I am making a mistake or is this a bug?
        • 1. Re: JMenuItem not receiving mouseReleased event when outside of JFrame
          UdayaShankaraGandhi
          Hi I am getting the action events triggered on the menu items which are outside of the JFrame when i release the mouse on them. You may release your mouse out of the menu item which is not triggering action event on the menu item you are exepecting to trigger.

          Here is the code written in BasicMenuItemUI class to process mouseRelase event performed on a JMenuItem
          public void mouseReleased(MouseEvent e){
            MenuSelectionManager manager = MenuSelectionManager.defaultManager();
            Point p = e.getPoint();                                                  // Point where you release your mouse
            
            if (p.x >=0 && p.x < menuItem.getWidth()                     // Checking whether your mouse release point
                     && p.y >=0 && menuItem.getHeight())                 // is exists inside the menuItem area or not.
            {
              doClick(manager);                                                      // If exists, perform the click event
            }
            else
            {
              manager.processMouseEvent(e);                                 // If not process it as normal mouse event
            }
          }
          So release your mouse on the menuItem you are expecting to trigger the action event
          • 2. Re: JMenuItem not receiving mouseReleased event when outside of JFrame
            805941
            Thanks for the quick reply.

            I think this issue might only reproduce on the openjdk. Have you run the code in the openjdk jre?