Core Architecture: Major System: Drag and Drop - UA-ScriptEase/scriptease GitHub Wiki
Swing uses TransferHandler
s to implement drag and drop. Write a custom TransferHandler
and attach it to the JComponent
you want to be dnd-enabled.
However, there are a few catches you should be aware of:
- A
JComponent
you want to be dnd-enabled needs to be on top of everything else to receive clicks. Usually this isn't a problem, but whenever you use aCellRenderer
, or other flyweight, it can become an issue. - A
JComponent
that has both aTransferHandler
and aMouseMotionListener
attached will never run theMouseMotionListener
code. This is apparently a bug Swing acknowledges with the Windows implementation of dnd, but they have no plans to fix it. - Drag and Drop is only enabled by default for certain
JComponents
: List of DnD enabled JComponents
General Information
Purpose: Swing TransferHandlers
are intended to make Drag and Drop operations easier to implement by using the Swing framework.
Javadoc Link: http://download.oracle.com/javase/6/docs/api/javax/swing/TransferHandler.html
Swing Drag and Drop Tutorial: http://download.oracle.com/javase/tutorial/uiswing/dnd/intro.html
Overview of TransferHandlers used in SE II
We've implemented several subclasses of TransferHandler to deal with the drag and drop needs of our interface:
SETreeTransferHandler
deals with the dragging and dropping of rows into and out of the main tree.BindingTransferHandler
deals with the dragging and dropping of bindings between binding slots within tree rows.BindingTransferHandlerExportOnly
deals with beginning a drag operation from the Custom picker for pinball.DefaultPickerTransferHandlerExportOnly
deals with beginning a drag operation from the default game object picker.LibraryTransferHandler
deals with importing of tree rows into the Library pane (promoting them to patterns) and exporting them into Stories.StartItTransferHandler
deals with dragging and dropping of Causes onto an Encounter.
How to Write TransferHandlers
- Create a new class and make it extend
TransferHandler
. - Override any of the linked methods to change the drag and drop behaviour: method list
Special Considerations
The Windows implementation of Swing drag and drop conflicts horribly with several MouseMotionListener
methods. If you have a TransferHandler
registered on a JComponent
, and you try to add a MouseMotionListener
(say because you want to draw a transparent ghost of the JComponent
alongside the mouse pointer as it's dragged, or something else equally reasonable and sane), the MouseMotionListener
methods will never fire because the event is consumed by the drag and drop implementation. Also, you can't modify the Swing drag and drop implementation because everything is private and references private classes, so we're stuck misusing the mouseClicked
method to begin a drag and drop operation. Thanks, Swing!