This following section discusses possible problems with data transfer features, which allows you to add drag and drop (DnD) and cut, copy and paste (CCP) operations to the application.
It is difficult to use a debugger to troubleshoot DnD features, because during the drag-and-drop operation all input is grabbed. Therefore, if you place a breakpoint during DnD, you might need to restart your X server. Try to use remote debugging instead.
Two simple methods can be used to troubleshoot most issues with DnD:
Printing all DataFlavor
instances
Printing received data
An alternative to remote debugging is the System.err.println()
function, which prints output without delay.
This section describes some issues that frequently arise with data transfer operations in AWT and suggests troubleshooting solutions.
Issue 1 - Pasting a huge amount of data from the clipboard takes too much time.
Using the Clipboard.getContents()
function for a paste operation sometimes causes the application to hang for a while, especially if a rich application provides the data to paste.
The Clipboard.getContents()
function fetches clipboard data in all available flavors (for example, some text and image flavors), and this can be expensive and unnecessary.
Solution: Use the Clipboard.getData()
method to get only specific data from the clipboard. If data in only one or a few flavors are needed, use one of the following Clipboard
methods instead of getContents()
:
DataFlavor[] getAvailableDataFlavors()
boolean isDataFlavorAvailable(DataFlavor flavor)
Object getData(DataFlavor flavor)
Issue 2 - When a Java application uses Transferable.getTransferData()
for DnD operations, the drag seems to take a long time.
In order to initialize transferred data only if it is needed, initialization code was put in Transferable.getTransferData()
.
Transferable
data is expensive to generate, and during a DnD operation Transferable.getTransferData()
is invoked more than once, causing a slowdown.
Solution: Cache the Transferable
data so that it is generated only once.
Issue 3 - Files cannot be transferred between a Java application and the GNOME/KDE desktop and file browser.
On Windows and some window managers, transferred file lists can be represented as DataFlavor.javaFileListFlavor
data flavor. But not all window managers represent lists of files in this format. For example, the GNOME window manager represents a file list as a list of URIs.
Workaround: To get files, request data of type String
, and then translate the string to a list of files according to text/uri-list format described in RFC 2483. To enable dropping files from a Java application to GNOME/KDE desktop and file browser, export data in the text/uri-list format. For a code example, see the Work Around section from the RFE.
Solution: Move a window with an image rendered on it as the mouse cursor moves during a DnD operation. See the code example in the Work Around section from the RFE.
Issue 4 - An image is passed to one of the startDrag()
methods of DragGestureEvent
or DragSource
, but the image is not displayed during the subsequent DnD operation.
Issue 5 - There is no way to transfer an array using DnD.
The DataFlavor
class has no constructor which handles arrays. The mime type for array contains characters which should be escaped. The code in Example 10-7 throws an IllegalArgumentException
.
Example 10-7 Code for IllegalArgumentException
new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + "; class=" + (new String[0]).getClass().getName())
Solution: Quote the value of the representation class parameter, as shown in Example 10-8, where the quotation marks are escaped:
Example 10-8 Code for Representation Class Parameter
new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + "; class=" + "\"" + (new String[0]).getClass().getName() + "\"")
For more information, see the this bug report.
Issue 6 - There are problems using AWT DnD support with Swing components.
Various problems can arise, for example, odd events are fired during a DnD operation, multiple items cannot be dragged and dropped, an InvalidDnDOperationException
is thrown.
Solution: Use Swing's DnD support with Swing components. Although the Swing DnD implementation is based on the AWT DnD implementation, you cannot mix Swing and AWT DnD. For more information, see the DnD section of the Swing Tutorial and Java Swing documentation.
Issue 7 - There is no way to change the state of the source to depend on the target.
In order to change the state of the source to depend on the target, you need to have references to the source and target components in the same area of code, but this is not currently implemented in the DnD API.
Workaround: One workaround is to add flags to the transferable object that allow you to determine the context of the event.
For the transfer of data within one Java VM, the following workaround is proposed:
Implement your target component as DragSourceListener
.
In DragGestureRecognizer.dragGestureRecognized()
add the target at drag source listener, as shown in Example 10-9.
Now you can get the target and the source in the dragEnter()
, dragOver()
, dropActionChanged()
, and dragDropEnd()
methods of DragSourceListener()
.
Issue 8 - Transferring of objects in an application takes a long time.
The transferring of a big bundle of data or the creation of transferred objects takes too long. The user must wait a long time for the data transfer to complete.
This expensive operation makes transferring too long because you must wait until Transferable.getTransferData()
finishes.
Solution: This solution is valid only for transferring data within one Java VM. Create or get expensive resources before the drag operation. For example, obtain file content when you create transferable, so that Transferable.getTransferData()
will not be too long.