Niels Hilbrink writes:
> I have been fiddling unsuccessfully with the Drag and Drop (dnd)
> features in Java.
Me too. No fix here, but a simpler test case that points to a problem
with Swing. The code below only works if class Source extends TextArea,
but not JTextArea (search for "blows"). The target of the drop can
be a Swing component with no errors.
Wandering through the source shows that native function
Java_sun_awt_motif_MDragSourceContextPeer_startDrag
returns null when it gets a NULL XEvent from the passed-in trigger
object (awt_XmDnD.c:843 unmodified jdk1.2 src). No idea why this is
different for JComponents versus Components.
-- Pete
/*
* Test DnD.
*/
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class Target extends JTextArea implements DropTargetListener {
private DropTarget target;
public Target(String text) {
super(text);
target = new DropTarget(this, DnDConstants.ACTION_COPY, this);
setDropTarget(target);
}
// Interface DropTargetListner
public void dragEnter(DropTargetDragEvent dtde) {
System.out.println("Target: dragEnter");
if (dtde.isDataFlavorSupported(DataFlavor.plainTextFlavor))
dtde.acceptDrag(DnDConstants.ACTION_COPY);
else {
dtde.rejectDrag();
}
}
public void dragOver(DropTargetDragEvent dtde) {
System.out.println("Target: dragOver");
}
public void dropActionChanged(DropTargetDragEvent dtde) {
System.out.println("Target: dropActionChanged");
}
public void dragScroll(DropTargetDragEvent dtde) {
System.out.println("Target: dragScroll");
}
public void dragExit(DropTargetEvent dte) {
System.out.println("Target: dragExit");
}
public void drop(DropTargetDropEvent dtde) {
System.out.println("Target: dropped");
dtde.acceptDrop(DnDConstants.ACTION_COPY);
StringReader r = null;
try {
r = (StringReader) dtde.getTransferable()
.getTransferData(DataFlavor.plainTextFlavor);
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
char[] buf = new char[64];
int i;
try {
do {
if ((i = r.read(buf)) > 0)
sb.append(new String(buf, 0, i));
} while (i >= 0);
} catch (Exception e) {
e.printStackTrace();
}
setText(sb.toString());
target.getDropTargetContext().dropComplete(true);
}
}
/*
* Source can't be JTextArea or it blows
*/
class Source extends TextArea
implements DragSourceListener, DragGestureListener {
private DragSource source;
public Source(String text) {
super(text);
source = DragSource.getDefaultDragSource();
source.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_COPY, this);
}
// Interface DragGestureListener
public void dragGestureRecognized(DragGestureEvent dge) {
System.out.println("Source: dragGestureRecognized");
try {
source.startDrag(dge, DragSource.DefaultCopyDrop,
null, new Point(0,0), new StringSelection(getText()), this);
} catch (InvalidDnDOperationException e) {
System.err.println("Source.dragGestureRecognized: " + e);
e.printStackTrace();
}
}
// Interface DragSourceListener
public void dragEnter(DragSourceDragEvent dsde) {
System.out.println("Source: dragEnter");
}
public void dragOver(DragSourceDragEvent dsde) {
System.out.println("Source: dragOver");
}
public void dropActionChanged(DragSourceDragEvent dsde) {
System.out.println("Source: dropActionChanged");
}
public void dragGestureChanged(DragSourceDragEvent dsde) {
System.out.println("Source: dragGestureChanged");
}
public void dragExit(DragSourceEvent dse) {
System.out.println("Source: dragExit");
}
public void dragDropEnd(DragSourceDropEvent dsde) {
System.out.println("Source: dragDropEnd");
}
}
public class dnd extends JFrame {
public dnd() {
super("DnD test");
Container c = getContentPane();
c.setLayout(new GridLayout(2, 1));
c.add(new Target("Target: drag the source here"));
c.add(new Source("Source: drag this to the target"));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
dnd f = new dnd();
f.setSize(600, 400);
f.setVisible(true);
while (true) {
try {
Thread.sleep(1000 * 1000);
} catch(InterruptedException e) { }
}
}
}
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]