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

JPopupMenu doesn't intercept events over JList

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5
    • client-libs
    • sparc
    • solaris_2.5.1



      Name: diC59631 Date: 01/23/98


      1. Just compile and run the program
          Press the right mouse button (Solaris) in
             the List (the first time
             the click will not succeed, don't know
             why)
          Hold the button down and drag it down the
          PopupMenu.

          Result: the list selection in the background
          is changing. Therefore when executing any
          action associated with the MenuItems the
          wrong selectionIndex might be used.


      2. SOURCE:

      import com.sun.java.swing.*;
      public class dummy extends JList
      {
         JPopupMenu popupMenu;
         public dummy ( String[] vals ) {
            super ( vals );
            popupMenu = new JPopupMenu();
            popupMenu.add( new JMenuItem ( "rename" ) );
            popupMenu.add( new JMenuItem ( "add" ) );
            popupMenu.add( new JMenuItem ( "delete" ) );
            popupMenu.pack();
            addMouseListener ( new PopupMouseListener() );
         }
         class PopupMouseListener extends java.awt.event.MouseAdapter
         {
            public void mousePressed ( java.awt.event.MouseEvent e ) {
               if (e.isPopupTrigger()) {
                  popup(e);
                  e.consume();
               }
            }
            public void mouseReleased ( java.awt.event.MouseEvent e ) {
               if (e.isPopupTrigger()) {
                  popup(e);
                  e.consume();
               }
            }
            void popup( java.awt.event.MouseEvent e ) {
               int index = locationToIndex (
                  new java.awt.Point(e.getX(), e.getY() ));
               popupMenu.show(dummy.this, e.getX(), e.getY());
            }
         }

         public static final void main ( String[] args ) {
            JFrame f = new JFrame();
            String[] sa = new String[] { "one", "two", "three", "four" };
            dummy l = new dummy ( sa );
            f.getContentPane().add( l );
            f.pack();
            f.show();
         }
      }


      3. -
      4. -
      5. Swing Version 0.7
      (Review ID: 23886)
      ======================================================================

            gsaab Georges Saab
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: