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

Mouse events to JPopupMenu are passed to underlaying components

XMLWordPrintable



      Name: dm26566 Date: 10/14/98


      /* This program shows a problem in Swing that has been there since before 1.0.2
       * and is still in JDK 1.2fcs-N where mouse events which are going to a JPopup
       * menu are leaked back to the underlaying component.
       *
       * To see this for yourself:
       * - compile and run this program
       * - select (click-drag-release) a part of the text (say the first two lines)
       * - move the mouse cursor down below the text, but still in the JTextArea
       * - display the JPopupMenu by using the right mouse button (click AND HOLD!)
       * - while still holding that button, move the cursor to the top and bottom of the
       * JTextArea
       * you will see that the text in the JTextArea is selected and unselected as the
       * mouse moves up and down, even if the events are also selecting different JMenuItems
       * in the JPopupMenu.
       *
       * If you had just popped up the JPopup with a right mouse click-and-release, you
       * would not see this behavior.
       *
       */

      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.*;

      public class jpopup extends JFrame implements MouseListener {
        private JComponent popupFromComponent = null;
        private JPopupMenu popup = null;

        public jpopup() {
            super ("Test Frame");
            addWindowListener( new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
            });

            // build the text area and put it in the frame....
            JTextArea area = new JTextArea("This\nis\nseveral\nlines\nof\ntest\ntext", 20, 20);
            
            this.getContentPane().setLayout( new BorderLayout());
            this.getContentPane().add(area);

            // This is the action listener for the menu item...
            ActionListener menuItemListener = new ActionListener() {
      public void actionPerformed( ActionEvent e) {
      System.out.println("The cursor was sitting on Component = " + popupFromComponent);
      }
            };

            // define and build the JPopup Menu....
            JMenuItem lookupBug = new JMenuItem("Do the first thing...");
            lookupBug.addActionListener(menuItemListener);

            JMenuItem miSave = new JMenuItem("Do the second thing...");
            miSave.addActionListener(menuItemListener);

            JMenuItem miSaveAll = new JMenuItem("Do the third thing...");
            miSaveAll.addActionListener(menuItemListener);

            JMenuItem miSaveSel = new JMenuItem("Do the fourth thing...");
            miSaveSel.addActionListener(menuItemListener);

            popup = new JPopupMenu();
            popup.add(lookupBug);
            popup.addSeparator();
            popup.add(miSave);
            popup.add(miSaveAll);
            popup.add(miSaveSel);

            // add the popup to the component...
            area.addMouseListener(this);

            // display the frame...
            pack();
            show();
        }
          // MouseListener interface:

          // This is the mouse listener to display the popup and store the component that the
          // mouse was over at "click-time"...
        public void mousePressed(MouseEvent event) {
            if (event.isPopupTrigger()) {
      popupFromComponent = (JComponent) event.getSource();
      popup.show(popupFromComponent, event.getX(), event.getY());
            }
        }
        public void mouseClicked(MouseEvent event) {
        }
        public void mouseReleased(MouseEvent event) {
        }
        public void mouseEntered(MouseEvent event) {
        }
        public void mouseExited(MouseEvent event) {
        }
        
        public static void main(String[] args) {
            jpopup p = new jpopup();
        }
      }
      (Review ID: 40475)
      ======================================================================

            gsaab Georges Saab
            dmcduffisunw Dale Mcduffie (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: