1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.JFrame;
public class DragDropBis implements DragGestureListener,
DragSourceListener,
DropTargetListener, Transferable {
static final DataFlavor[] supportedFlavors = {null};
static {
try {
supportedFlavors[0] = new
DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Object object;
// Transferable methods.
public Object getTransferData(DataFlavor flavor) {
if (flavor.isMimeTypeEqual
(DataFlavor.javaJVMLocalObjectMimeType)) {
return object;
} else {
return null;
}
}
public DataFlavor[] getTransferDataFlavors() {
return supportedFlavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.isMimeTypeEqual
(DataFlavor.javaJVMLocalObjectMimeType);
}
// DragGestureListener method.
public void dragGestureRecognized(DragGestureEvent ev) {
ev.startDrag(null, this, this);
}
// DragSourceListener methods.
public void dragDropEnd(DragSourceDropEvent ev) {
}
public void dragEnter(DragSourceDragEvent ev) {
}
public void dragExit(DragSourceEvent ev) {
}
public void dragOver(DragSourceDragEvent ev) {
object = ev.getSource();
}
public void dropActionChanged(DragSourceDragEvent ev) {
}
// DropTargetListener methods.
public void dragEnter(DropTargetDragEvent ev) {
}
public void dragExit(DropTargetEvent ev) {
}
public void dragOver(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}
public void dropActionChanged(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}
void dropTargetDrag(DropTargetDragEvent ev) {
ev.acceptDrag(ev.getDropAction());
}
public void drop(DropTargetDropEvent ev) {
ev.acceptDrop(ev.getDropAction());
try {
Object target = ev.getSource();
Object source = ev.getTransferable().getTransferData (supportedFlavors[0]);
Component component = ((DragSourceContext) source).getComponent();
Container oldContainer = component.getParent();
Container container = (Container) ((DropTarget)target).getComponent();
container.add(component);
oldContainer.validate();
oldContainer.repaint();
container.validate();
container.repaint();
}
catch (Exception ex) {
ex.printStackTrace();
}
ev.dropComplete(true);
}
@SuppressWarnings("deprecation")
public static void main(String[] arg) {
Button buttonSource = new Button("A");
Button buttonTarget = new Button("B");
JFrame frame = new JFrame("test");
frame.setLayout(new FlowLayout());
frame.add(buttonSource);
frame.add(buttonTarget);
DragDropBis dndListener = new DragDropBis();
@SuppressWarnings("unused")
DropTarget dropTarget = new DropTarget(frame, DnDConstants.ACTION_MOVE, dndListener);
@SuppressWarnings("unused")
DropTarget dropSource = new DropTarget(frame, DnDConstants.ACTION_MOVE, dndListener);
DragSource dragSource = new DragSource();
DragSource dragTarget = new DragSource();
@SuppressWarnings("unused")
DragGestureRecognizer dragRecognizer1 = dragSource.createDefaultDragGestureRecognizer(buttonSource, DnDConstants.ACTION_MOVE, dndListener);
@SuppressWarnings("unused")
DragGestureRecognizer dragRecognizer2 = dragTarget.createDefaultDragGestureRecognizer(buttonTarget, DnDConstants.ACTION_MOVE, dndListener);
frame.setBounds(0, 100, 400, 100);
frame.show();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
} |
Partager