Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6481191

Unexpected MouseClicked() event when JPopUpMenu opened partly outside of Frame

XMLWordPrintable

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

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Professional Version 2002 Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      I get an unexpected mouseClicked() event, when opening a JPopUpMenu on a JPanel. This happens when the JPopUpMenu opens partly outside of the Frame.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the attached test code. The code opens a dialog, where you can open a popupmenu by clicking it. When you open the popup close to the bottom of the dialog so that it opens partly outside of the dialog you get an extra mouseClicked() event. (The MouseEvents are printed to the console). In my opinion this should not happen.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to get the same MouseEvents recardles of the place where the JPopUpMenu is opened.
      ACTUAL -
      I get an extra mouseClicked() event, when popup is opened partly outside the dialog.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Class 1:
      -----------------------------------------------

      package eventbug;

      import java.awt.Toolkit;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;
      import java.awt.Dimension;

      public class Test {
          boolean packFrame = false;

          /**
           * Construct and show the application.
           */
          public Test() {
              Frame frame = new Frame();
              // Validate frames that have preset sizes
              // Pack frames that have useful preferred size info, e.g. from their layout
              if (packFrame) {
                  frame.pack();
              } else {
                  frame.validate();
              }

              // Center the window
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize = frame.getSize();
              if (frameSize.height > screenSize.height) {
                  frameSize.height = screenSize.height;
              }
              if (frameSize.width > screenSize.width) {
                  frameSize.width = screenSize.width;
              }
              frame.setLocation((screenSize.width - frameSize.width) / 2,
                                (screenSize.height - frameSize.height) / 2);
              frame.setVisible(true);
          }

          /**
           * Application entry point.
           *
           * @param args String[]
           */
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      try {
                          UIManager.setLookAndFeel(UIManager.
                                                   getSystemLookAndFeelClassName());
                      } catch (Exception exception) {
                          exception.printStackTrace();
                      }

                      new Test();
                  }
              });
          }
      }


      Class 2: ------------------------

      package eventbug;

      import java.awt.BorderLayout;
      import java.awt.Dimension;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import java.awt.event.MouseListener;
      import java.awt.event.MouseEvent;
      import javax.swing.JPopupMenu;
      import javax.swing.JMenuItem;

      public class Frame extends JFrame implements MouseListener{
          JPanel contentPane;
          BorderLayout borderLayout1 = new BorderLayout();
          JPopupMenu pop = new JPopupMenu();



          public Frame() {
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              contentPane = (JPanel) getContentPane();
              contentPane.setLayout(borderLayout1);
              contentPane.addMouseListener(this);
              setSize(new Dimension(400, 300));
              setTitle("Event bug test");
              JMenuItem item1 = new JMenuItem("MenuItem1");
              JMenuItem item2 = new JMenuItem("MenuItem2");
              JMenuItem item3 = new JMenuItem("MenuItem3");
              JMenuItem item4 = new JMenuItem("MenuItem4");
              pop.add(item1);
              pop.add(item2);
              pop.add(item3);
              pop.add(item4);
          }

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

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

          public void mouseReleased(MouseEvent e) {
              System.out.println("mouseReleased");
              if (e.isPopupTrigger()) {
                  pop.show(this, e.getX(), e.getY());;
              }
          }

          public void mouseEntered(MouseEvent e) {
          }

          public void mouseExited(MouseEvent e) {
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: