
method of the Transferable object to actually transfer the data. This phase of
the data transfer is done exactly as it is in cut-and-paste. Finally, the compo-
nent calls the
dropComplete() method of the DropTargetDropEvent, passing
true if the transfer was successful or false if something went wrong and the
transfer did not succeed.
A Data Source
Example 6-2 shows the ColorSource class. This is a simple JComponent subclass
that displays a small block of a solid color and makes that color available for trans-
fer via both cut-and-paste and drag-and-drop. The
copy() method copies the color
to the clipboard, making it available for pasting, while the
dragGestureRecog-
nized()
method initiates a drag operation. This example relies upon the Trans-
ferableColor class of Example 6-1, of course. For simplicity, the copy() method is
invoked when the user clicks on the component—ther e is no Ctrl-C keyboard
binding or Edit menu command.
Example 6−2: ColorSour ce.java
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
/**
* This simple component displays a solid color and allows that color
*tobedragged. Also, it copies the color to the clipboard when the
* user clicks on it.
*/
public class ColorSource extends JComponent
implements ClipboardOwner, DragGestureListener, DragSourceListener
{
Color color; // ...