| 
                  Drag and Drop
                   in Java  | 
            
| 
                   
                      
                     Prof. David Bernstein
                       | 
            
| Computer Science Department | 
| bernstdh@jmu.edu | 
               
            
         
            
         
         
            
         Object that implements the
          Transferable
                  
                     
                   interface
          DataFlavor
                  
                     
                   that
          describes the Transferable
               Component that has an associated 
          DragSource
                  
                     
                  
               DragGestureListener
                  
                     
                   that is
          informed when a drag gesture is recognized by a
          DragSource
               Component that has an associated 
          DropTarget
                  
                     
                  
               TransferHandler
                  
                     
                   that handles
          the transfer of the Transferable to/from the
          Component objects.
         
            
         getTransferHandler() method is invoked
    TransferHandler object's
          createTransferable() method is invoked
          which results in a call to the source object's
          get____() method (where ____
          denotes the name of the attribute)
    TransferHandler object's 
          getSourceActions() and 
          canImport() methods are invokedComponent object's
          set____() method is invoked and the 
          source object's exportDone() method
          is invoked
         
            
         setDragEnabled(true) 
              the following components recognize the drag gesture:
              JColorChooser, JEditorPane,
              JFileChooser, JFormattedTextField,
              JList, JTable, 
              JTextArea, JTextField, 
              JTextPane, and JTree
                     JEditorPane,
              JFormattedTextField,
              JPasswordField,
              JTextArea, JTextField, 
              and JTextPane
                     JList, JTable,
              JTree
                     
         
            
         Transferable:
      java.awt.datatransfer.StringSelection
                     DataFlavor:
      DataFlavor.stringFlavor
                     JList (with a
              java.awt.dnd.DragSource) 
              and a JTextArea (with a
              javax.swing.TransferHandler.SwingDropTarget)TransferHandler:
      javax.swing.plaf.basic.BasicTextUI.TextTransferHandler
                     
         
            
         TransferHandler
              can be used to transfer String objects
              to/from components that have the "text" property
              (i.e., getText() and setText()
              methods) using DataFlavor.stringFlavor
                     
         
            
         JLabel has getText() and
              setText() methodssetTransferHandler()
                     TransferHandler
                     
         
            
         
         
            
         TransferHandler
         
         
            
         Component that can display words
              at random locations (like a collage)String and a locationJPanel
                     TransferHandler
                     TransferHandler (cont.)
         
            
         TransferHandler (cont.)
         
            
         TransferHandler (cont.)
         
            
         
         
            
         TransferHandler
                     DropTargetListener
              and a DropTarget to register
              it withTransferHandler for other purposes later,
              let's use the second approach nowStringCollage implement
              DropTargetListener
                     
         
            
         TransferHandler
         
         
            
         Component that 
              displays words at different locations (like a collage)TransferHandler
              that a drag gesture has startedDragGestureRecognizer
              and a DragSource to register
              it withMouseListener
                     DragSource later, we'll
              use the second approach nowMouseListener will only be used
              locally and once, we'll use an anonymous classTransferHandler (cont.)
         
            
         TransferHandler
         
         
            
         Component that 
              displays words at different locations (like a collage)TransferHandler
                     DragGestureListener and
              DragSourceListener
              and a DragSource to register
              them withTransferHandler later
              so will use the second approach nowStringCollage implement
              DragGestureListener and
              DragSourceListener
                     TransferHandler (cont.)
         
            
         TransferHandler
         
         
            
         Component that 
              displays words at different locations (like a collage)TransferHandler
              that controls the processTransferHandler (cont.)
         
            
         StringTransferHandler
            TransferHandler (cont.)
         
            
         
         
            
         TransferHandler
              and DataFlavor
                     TransferHndler
                     TransferHndler
                     
         
            
         TransferHandler 
         
         
            
         TransferHandler
         
         
            
         TransferHandler
         
         
            
         
         
            
         Transferable interfaceStringSelection class) for this
               purposeTransferable version of the object of
              interest
         
            
         Transferable interfaceTransferable Functionality:
      Transferable interfaceTransferable Image
         
            
         Image is an abstract class, so we must 
              extend either BufferedImage or 
              VolatileImage which is limitingImage isn't an interface so the
              decorator must extend it rather than implement it
              which can be confusingTransferable Image (cont.)
         
            
         StringSelection?
      String
              objects so nothing new is requiredTransferHandler
         
         
            
         Transferable (which must use a local
              object reference)TransferHandler (cont.)
         
            
         Transferable
            TransferHandler (cont.)
         
            
         TransferHandler
         
         
            
         Transferable (which can now use
              a more flexible DataFlavor)TransferHandler
                     TransferHandler (cont.)
         
            
         TransferHandler (cont.)
         
            
         Transferable
            TransferHandler (cont.)
         
            
         TransferHandler
            TransferHandler (cont.)
         
            
         
         
            
         Appointment (which has Date 
              and String attributes) from one GUI component
              to anotherAppointment class (which implements
              Transferable)TransferHandler
                     
         
            
         Appoinment Class
         
            
         
         
            
         TransferHandler
            
         
            
         
         
            
         
         
            
         TransferHandler:
      String representation for the transfer
              and create/parse as needed
              (with
               DataFlavor.stringFlavor
              )DataFlavor.javaSerializedObjectMimeType
              )new DataFlavor("application/x-java-jvm-local-objectref;class=Appointment")
              )TransferHandler:
      DragSource and or DropTarget)