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

ctrl+mouse-press+drag+release-crtl=drop

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.2
    • client-libs
    • beta2
    • x86
    • windows_98



      Name: vi73552 Date: 07/01/99


      When the user ctrl+'left mouse click' a draggable object and then drags the object to the drop target and then releases the ctrl key (without releasing the mouse button), the draggable object is actually droppped onto the target. I think this is related to bug# 4218517, but in this case the mouse is not affected.

      I've provided an example of a draggable JLabel that can be dropped onto a JFrame. The transferred object is only the JLabel's text, and when dropped a new non-draggable JLabel is created in the frame at the drop location. The only drag 'n' drop operation supported is ACTION_MOVE.

      What happens is that the drop succeeds when the user does a ctrl+'left mouse click'+drag+'release ctrl key' and a new JLabel is created.


      //====================================================
      // SOURCE CODE
      //====================================================
      import java.awt.Color;
      import java.awt.datatransfer.*;
      import java.awt.dnd.*;
      import javax.swing.JFrame;
      import javax.swing.JLabel;


      //====================================================
      // BugTest
      //====================================================
      public class BugTest
      {
         //------------------------------------------------
         public static final void main( String[] argv )
         {
            DropFrame frame;
            
            frame = new DropFrame( "DnD Frame" );
            frame.show();
         }
      }


      //====================================================
      // DragLabel
      //====================================================
      class DragLabel extends JLabel
                        implements DragGestureListener,
                                    DragSourceListener
      {
         //-------------------------------------------------
         public DragLabel()
         {
            // Label GUI set-up
            super( "Drag Me!" );
            setBounds( 45, 45, 60, 20 );
            setBackground( Color.yellow );
            setOpaque( true );

            // Label Drag 'n' Drop set-up
      DragSource source;

            source = new DragSource();
      source.createDefaultDragGestureRecognizer(
                this, DnDConstants.ACTION_MOVE, this );
         }
         
         //-------------------------------------------------
         // DragGestureListener methods
         public void dragGestureRecognized( DragGestureEvent evt )
         {
            evt.startDrag( DragSource.DefaultMoveDrop,
                           new StringSelection( getText() ),
                           this );
         }

         //-------------------------------------------------
         // DragSourceListener methods
         public void dragDropEnd( DragSourceDropEvent evt ) {}
         public void dragEnter( DragSourceDragEvent evt ) {}
         public void dragExit( DragSourceEvent evt ) {}
         public void dragOver( DragSourceDragEvent evt ) {}
         public void dropActionChanged( DragSourceDragEvent evt ) {}
      }


      //====================================================
      // DropFrame
      //====================================================
      class DropFrame extends JFrame
                        implements DropTargetListener
      {
         //-------------------------------------------------
         public DropFrame( String title )
         {
            // Frame GUI set-up
            super( title );
            setSize( 300, 300 );
            getContentPane().setLayout( null );
            getContentPane().add( new DragLabel() );

            // Frame Drag 'n' Drop set-up
            new DropTarget( getContentPane(),
                            DnDConstants.ACTION_MOVE,
                            this );
         }
         
         //-------------------------------------------------
         // DropTargetListener methods
         public void dragExit( DropTargetEvent evt ) {}
         public void dragOver( DropTargetDragEvent evt ) {}
         public void dropActionChanged( DropTargetDragEvent evt ) {}
         
         //-------------------------------------------------
         public void dragEnter( DropTargetDragEvent evt )
         {
            evt.acceptDrag( DnDConstants.ACTION_MOVE );
         }

         //-------------------------------------------------
         public void drop( DropTargetDropEvent evt )
         {
            try
            {
               DataFlavor df = DataFlavor.stringFlavor;

               if( evt.isDataFlavorSupported( df ) && evt.getDropAction() == DnDConstants.ACTION_MOVE )
               {
                  String text;
                  JLabel newLab;

                  evt.acceptDrop( DnDConstants.ACTION_MOVE );

                  // create new JLabel from transferred string
                  text = (String )evt.getTransferable().getTransferData( df );
                  newLab = new JLabel( text );
                  newLab.setSize( 60, 20 );
                  newLab.setLocation( evt.getLocation() );

                  // update frame GUI
                  getContentPane().add( newLab );
                  getContentPane().repaint();
                        
                  evt.dropComplete( true );
               }
               else
                  evt.rejectDrop();
            }
            catch( Exception err ) {
               evt.dropComplete( false );
               err.printStackTrace();
            }
         }
      }
      (Review ID: 85103)
      ======================================================================

            dassunw Das Das (Inactive)
            vasya Vassili Igouchkine (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: