- All Implemented Interfaces:
- Serializable
public class DragSource extends Object implements Serializable
DragSource is the entity responsible
 for the initiation of the Drag
 and Drop operation, and may be used in a number of scenarios:
 - 1 default instance per JVM for the lifetime of that JVM.
- 1 instance per class of potential Drag Initiator object (e.g TextField). [implementation dependent]
- 1 per instance of a particular
 Component, or application specific object associated with aComponentinstance in the GUI. [implementation dependent]
- Some other arbitrary association. [implementation dependent]
DragSource is
 obtained, a DragGestureRecognizer should
 also be obtained to associate the DragSource
 with a particular
 Component.
 
 The initial interpretation of the user's gesture,
 and the subsequent starting of the drag operation
 are the responsibility of the implementing
 Component, which is usually
 implemented by a DragGestureRecognizer.
 When a drag gesture occurs, the
 DragSource's
 startDrag() method shall be
 invoked in order to cause processing
 of the user's navigational
 gestures and delivery of Drag and Drop
 protocol notifications. A
 DragSource shall only
 permit a single Drag and Drop operation to be
 current at any one time, and shall
 reject any further startDrag() requests
 by throwing an IllegalDnDOperationException
 until such time as the extant operation is complete.
 
 The startDrag() method invokes the
 createDragSourceContext() method to
 instantiate an appropriate
 DragSourceContext
 and associate the DragSourceContextPeer
 with that.
 
 If the Drag and Drop System is
 unable to initiate a drag operation for
 some reason, the startDrag() method throws
 a java.awt.dnd.InvalidDnDOperationException
 to signal such a condition. Typically this
 exception is thrown when the underlying platform
 system is either not in a state to
 initiate a drag, or the parameters specified are invalid.
 
 Note that during the drag, the
 set of operations exposed by the source
 at the start of the drag operation may not change
 until the operation is complete.
 The operation(s) are constant for the
 duration of the operation with respect to the
 DragSource.
- Since:
- 1.2
- See Also:
- Serialized Form
- 
Field SummaryFields Modifier and Type Field Description static CursorDefaultCopyDropThe defaultCursorto use with a copy operation indicating that a drop is currently allowed.static CursorDefaultCopyNoDropThe defaultCursorto use with a copy operation indicating that a drop is currently not allowed.static CursorDefaultLinkDropThe defaultCursorto use with a link operation indicating that a drop is currently allowed.static CursorDefaultLinkNoDropThe defaultCursorto use with a link operation indicating that a drop is currently not allowed.static CursorDefaultMoveDropThe defaultCursorto use with a move operation indicating that a drop is currently allowed.static CursorDefaultMoveNoDropThe defaultCursorto use with a move operation indicating that a drop is currently not allowed.
- 
Constructor SummaryConstructors Constructor Description DragSource()Creates a newDragSource.
- 
Method SummaryModifier and Type Method Description voidaddDragSourceListener(DragSourceListener dsl)Adds the specifiedDragSourceListenerto thisDragSourceto receive drag source events during drag operations initiated with thisDragSource.voidaddDragSourceMotionListener(DragSourceMotionListener dsml)Adds the specifiedDragSourceMotionListenerto thisDragSourceto receive drag motion events during drag operations initiated with thisDragSource.DragGestureRecognizercreateDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl)Creates a newDragGestureRecognizerthat implements the default abstract subclass ofDragGestureRecognizerfor thisDragSource, and sets the specifiedComponentandDragGestureListeneron the newly created object.<T extends DragGestureRecognizer>
 TcreateDragGestureRecognizer(Class<T> recognizerAbstractClass, Component c, int actions, DragGestureListener dgl)Creates a newDragGestureRecognizerthat implements the specified abstract subclass ofDragGestureRecognizer, and sets the specifiedComponentandDragGestureListeneron the newly created object.protected DragSourceContextcreateDragSourceContext(DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl)Creates theDragSourceContextto handle the current drag operation.static DragSourcegetDefaultDragSource()Gets theDragSourceobject associated with the underlying platform.DragSourceListener[]getDragSourceListeners()Gets all theDragSourceListeners registered with thisDragSource.DragSourceMotionListener[]getDragSourceMotionListeners()Gets all of theDragSourceMotionListeners registered with thisDragSource.static intgetDragThreshold()Returns the drag gesture motion threshold.FlavorMapgetFlavorMap()This method returns theFlavorMapfor thisDragSource.<T extends EventListener>
 T[]getListeners(Class<T> listenerType)Gets all the objects currently registered asFooListeners upon thisDragSource.static booleanisDragImageSupported()Reports whether or not dragImagesupport is available on the underlying platform.voidremoveDragSourceListener(DragSourceListener dsl)Removes the specifiedDragSourceListenerfrom thisDragSource.voidremoveDragSourceMotionListener(DragSourceMotionListener dsml)Removes the specifiedDragSourceMotionListenerfrom thisDragSource.voidstartDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl)Start a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theTransferablesubject data of the drag, and theDragSourceListener.voidstartDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap)Start a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theTransferablesubject data of the drag, theDragSourceListener, and theFlavorMap.voidstartDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point dragOffset, Transferable transferable, DragSourceListener dsl)Start a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theImageto drag, the offset of theImageorigin from the hotspot of theCursorat the instant of the trigger, the subject data of the drag, and theDragSourceListener.voidstartDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap)Start a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theImageto drag, the offset of theImageorigin from the hotspot of theCursorat the instant of the trigger, theTransferablesubject data of the drag, theDragSourceListener, and theFlavorMap.
- 
Field Details- 
DefaultCopyDropThe defaultCursorto use with a copy operation indicating that a drop is currently allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
DefaultMoveDropThe defaultCursorto use with a move operation indicating that a drop is currently allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
DefaultLinkDropThe defaultCursorto use with a link operation indicating that a drop is currently allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
DefaultCopyNoDropThe defaultCursorto use with a copy operation indicating that a drop is currently not allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
DefaultMoveNoDropThe defaultCursorto use with a move operation indicating that a drop is currently not allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
DefaultLinkNoDropThe defaultCursorto use with a link operation indicating that a drop is currently not allowed.nullifGraphicsEnvironment.isHeadless()returnstrue.- See Also:
- GraphicsEnvironment.isHeadless()
 
 
- 
- 
Constructor Details- 
DragSourceCreates a newDragSource.- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true
- See Also:
- GraphicsEnvironment.isHeadless()
 
 
- 
- 
Method Details- 
getDefaultDragSourceGets theDragSourceobject associated with the underlying platform.- Returns:
- the platform DragSource
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true
- See Also:
- GraphicsEnvironment.isHeadless()
 
- 
isDragImageSupportedpublic static boolean isDragImageSupported()Reports whether or not dragImagesupport is available on the underlying platform.- Returns:
- if the Drag Image support is available on this platform
 
- 
startDragpublic void startDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap) throws InvalidDnDOperationExceptionStart a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theImageto drag, the offset of theImageorigin from the hotspot of theCursorat the instant of the trigger, theTransferablesubject data of the drag, theDragSourceListener, and theFlavorMap.- Parameters:
- trigger- the- DragGestureEventthat initiated the drag
- dragCursor- the initial- Cursorfor this drag operation or- nullfor the default cursor handling; see DragSourceContext for more details on the cursor handling mechanism during drag and drop
- dragImage- the image to drag or- null
- imageOffset- the offset of the- Imageorigin from the hotspot of the- Cursorat the instant of the trigger
- transferable- the subject data of the drag
- dsl- the- DragSourceListener
- flavorMap- the- FlavorMapto use, or- null
- Throws:
- InvalidDnDOperationException- if the Drag and Drop system is unable to initiate a drag operation, or if the user attempts to start a drag while an existing drag operation is still executing
 
- 
startDragpublic void startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap) throws InvalidDnDOperationExceptionStart a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theTransferablesubject data of the drag, theDragSourceListener, and theFlavorMap.- Parameters:
- trigger- the- DragGestureEventthat initiated the drag
- dragCursor- the initial- Cursorfor this drag operation or- nullfor the default cursor handling; see DragSourceContext for more details on the cursor handling mechanism during drag and drop
- transferable- the subject data of the drag
- dsl- the- DragSourceListener
- flavorMap- the- FlavorMapto use or- null
- Throws:
- InvalidDnDOperationException- if the Drag and Drop system is unable to initiate a drag operation, or if the user attempts to start a drag while an existing drag operation is still executing
 
- 
startDragpublic void startDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point dragOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationExceptionStart a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theImageto drag, the offset of theImageorigin from the hotspot of theCursorat the instant of the trigger, the subject data of the drag, and theDragSourceListener.- Parameters:
- trigger- the- DragGestureEventthat initiated the drag
- dragCursor- the initial- Cursorfor this drag operation or- nullfor the default cursor handling; see DragSourceContext for more details on the cursor handling mechanism during drag and drop
- dragImage- the- Imageto drag or- null
- dragOffset- the offset of the- Imageorigin from the hotspot of the- Cursorat the instant of the trigger
- transferable- the subject data of the drag
- dsl- the- DragSourceListener
- Throws:
- InvalidDnDOperationException- if the Drag and Drop system is unable to initiate a drag operation, or if the user attempts to start a drag while an existing drag operation is still executing
 
- 
startDragpublic void startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationExceptionStart a drag, given theDragGestureEventthat initiated the drag, the initialCursorto use, theTransferablesubject data of the drag, and theDragSourceListener.- Parameters:
- trigger- the- DragGestureEventthat initiated the drag
- dragCursor- the initial- Cursorfor this drag operation or- nullfor the default cursor handling; see DragSourceContext class for more details on the cursor handling mechanism during drag and drop
- transferable- the subject data of the drag
- dsl- the- DragSourceListener
- Throws:
- InvalidDnDOperationException- if the Drag and Drop system is unable to initiate a drag operation, or if the user attempts to start a drag while an existing drag operation is still executing
 
- 
createDragSourceContextprotected DragSourceContext createDragSourceContext(DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl)Creates theDragSourceContextto handle the current drag operation.To incorporate a new DragSourceContextsubclass, subclassDragSourceand override this method.If dragImageisnull, no image is used to represent the drag over feedback for this drag operation, butNullPointerExceptionis not thrown.If dslisnull, no drag source listener is registered with the createdDragSourceContext, butNullPointerExceptionis not thrown.- Parameters:
- dgl- The- DragGestureEventthat triggered the drag
- dragCursor- The initial- Cursorfor this drag operation or- nullfor the default cursor handling; see DragSourceContext class for more details on the cursor handling mechanism during drag and drop
- dragImage- The- Imageto drag or- null
- imageOffset- The offset of the- Imageorigin from the hotspot of the cursor at the instant of the trigger
- t- The subject data of the drag
- dsl- The- DragSourceListener
- Returns:
- the DragSourceContext
- Throws:
- NullPointerException- if- dscpis- null
- NullPointerException- if- dglis- null
- NullPointerException- if- dragImageis not- nulland- imageOffsetis- null
- NullPointerException- if- tis- null
- IllegalArgumentException- if the- Componentassociated with the trigger event is- null.
- IllegalArgumentException- if the- DragSourcefor the trigger event is- null.
- IllegalArgumentException- if the drag action for the trigger event is- DnDConstants.ACTION_NONE.
- IllegalArgumentException- if the source actions for the- DragGestureRecognizerassociated with the trigger event are equal to- DnDConstants.ACTION_NONE.
 
- 
getFlavorMapThis method returns theFlavorMapfor thisDragSource.- Returns:
- the FlavorMapfor thisDragSource
 
- 
createDragGestureRecognizerpublic <T extends DragGestureRecognizer> T createDragGestureRecognizer(Class<T> recognizerAbstractClass, Component c, int actions, DragGestureListener dgl)Creates a newDragGestureRecognizerthat implements the specified abstract subclass ofDragGestureRecognizer, and sets the specifiedComponentandDragGestureListeneron the newly created object.- Type Parameters:
- T- the type of- DragGestureRecognizerto create
- Parameters:
- recognizerAbstractClass- the requested abstract type
- actions- the permitted source drag actions
- c- the- Componenttarget
- dgl- the- DragGestureListenerto notify
- Returns:
- the new DragGestureRecognizerornullif theToolkit.createDragGestureRecognizermethod has no implementation available for the requestedDragGestureRecognizersubclass and returnsnull
 
- 
createDefaultDragGestureRecognizerpublic DragGestureRecognizer createDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl)Creates a newDragGestureRecognizerthat implements the default abstract subclass ofDragGestureRecognizerfor thisDragSource, and sets the specifiedComponentandDragGestureListeneron the newly created object. For thisDragSourcethe default isMouseDragGestureRecognizer.- Parameters:
- c- the- Componenttarget for the recognizer
- actions- the permitted source actions
- dgl- the- DragGestureListenerto notify
- Returns:
- the new DragGestureRecognizerornullif theToolkit.createDragGestureRecognizermethod has no implementation available for the requestedDragGestureRecognizersubclass and returnsnull
 
- 
addDragSourceListenerAdds the specifiedDragSourceListenerto thisDragSourceto receive drag source events during drag operations initiated with thisDragSource. If anulllistener is specified, no action is taken and no exception is thrown.- Parameters:
- dsl- the- DragSourceListenerto add
- Since:
- 1.4
- See Also:
- removeDragSourceListener(java.awt.dnd.DragSourceListener),- getDragSourceListeners()
 
- 
removeDragSourceListenerRemoves the specifiedDragSourceListenerfrom thisDragSource. If anulllistener is specified, no action is taken and no exception is thrown. If the listener specified by the argument was not previously added to thisDragSource, no action is taken and no exception is thrown.- Parameters:
- dsl- the- DragSourceListenerto remove
- Since:
- 1.4
- See Also:
- addDragSourceListener(java.awt.dnd.DragSourceListener),- getDragSourceListeners()
 
- 
getDragSourceListenersGets all theDragSourceListeners registered with thisDragSource.- Returns:
- all of this DragSource'sDragSourceListeners or an empty array if no such listeners are currently registered
- Since:
- 1.4
- See Also:
- addDragSourceListener(java.awt.dnd.DragSourceListener),- removeDragSourceListener(java.awt.dnd.DragSourceListener)
 
- 
addDragSourceMotionListenerAdds the specifiedDragSourceMotionListenerto thisDragSourceto receive drag motion events during drag operations initiated with thisDragSource. If anulllistener is specified, no action is taken and no exception is thrown.- Parameters:
- dsml- the- DragSourceMotionListenerto add
- Since:
- 1.4
- See Also:
- removeDragSourceMotionListener(java.awt.dnd.DragSourceMotionListener),- getDragSourceMotionListeners()
 
- 
removeDragSourceMotionListenerRemoves the specifiedDragSourceMotionListenerfrom thisDragSource. If anulllistener is specified, no action is taken and no exception is thrown. If the listener specified by the argument was not previously added to thisDragSource, no action is taken and no exception is thrown.- Parameters:
- dsml- the- DragSourceMotionListenerto remove
- Since:
- 1.4
- See Also:
- addDragSourceMotionListener(java.awt.dnd.DragSourceMotionListener),- getDragSourceMotionListeners()
 
- 
getDragSourceMotionListenersGets all of theDragSourceMotionListeners registered with thisDragSource.- Returns:
- all of this DragSource'sDragSourceMotionListeners or an empty array if no such listeners are currently registered
- Since:
- 1.4
- See Also:
- addDragSourceMotionListener(java.awt.dnd.DragSourceMotionListener),- removeDragSourceMotionListener(java.awt.dnd.DragSourceMotionListener)
 
- 
getListenersGets all the objects currently registered asFooListeners upon thisDragSource.FooListeners are registered using theaddFooListenermethod.- Type Parameters:
- T- the type of listener objects
- Parameters:
- listenerType- the type of listeners requested; this parameter should specify an interface that descends from- java.util.EventListener
- Returns:
- an array of all objects registered as
          FooListeners on thisDragSource, or an empty array if no such listeners have been added
- Throws:
- ClassCastException- if- listenerTypedoesn't specify a class or interface that implements- java.util.EventListener
- Since:
- 1.4
- See Also:
- getDragSourceListeners(),- getDragSourceMotionListeners()
 
- 
getDragThresholdpublic static int getDragThreshold()Returns the drag gesture motion threshold. The drag gesture motion threshold defines the recommended behavior forMouseDragGestureRecognizers.If the system property awt.dnd.drag.thresholdis set to a positive integer, this method returns the value of the system property; otherwise if a pertinent desktop property is available and supported by the implementation of the Java platform, this method returns the value of that property; otherwise this method returns some default value. The pertinent desktop property can be queried usingjava.awt.Toolkit.getDesktopProperty("DnD.gestureMotionThreshold").- Returns:
- the drag gesture motion threshold
- Since:
- 1.5
- See Also:
- MouseDragGestureRecognizer
 
 
-