Java Example Program/ Sample Source Code
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GetDropActionExample implements DragGestureListener,
DragSourceListener, DropTargetListener, Transferable {
static final DataFlavor[] dataflavor = { null };
Object object;
static {
try {
dataflavor[0] = new DataFlavor(
DataFlavor.javaJVMLocalObjectMimeType);
} catch (Exception e) {
e.printStackTrace();
}
}
// Transferable methods.
public Object getTransferData(DataFlavor flavor) {
if (flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) {
return object;
} else {
return null;
}
}
public DataFlavor[] getTransferDataFlavors() {
return dataflavor;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType);
}
// DragGestureListener method.
public void dragGestureRecognized(DragGestureEvent dge) {
dge.startDrag(null, this, this);
}
// DragSourceListener methods.
public void dragDropEnd(DragSourceDropEvent dsde) {
System.out.println("DragSourceDropEvent Fired");
}
public void dragEnter(DragSourceDragEvent dsde) {
System.out.println("DragSourceDragEvent Fired");
}
public void dragExit(DragSourceEvent dse) {
System.out.println("DragSourceEvent Fired");
}
public void dragOver(DragSourceDragEvent dsde) {
object = dsde.getSource();
}
public void dropActionChanged(DragSourceDragEvent dsde) {
System.out.println("DragSourceDragEvent Fired");
}
// DropTargetListener methods.
public void dragEnter(DropTargetDragEvent dtde) {
}
public void dragExit(DropTargetEvent dte) {
}
public void dragOver(DropTargetDragEvent dtde) {
dropTargetDrag(dtde);
}
public void dropActionChanged(DropTargetDragEvent dtde) {
dropTargetDrag(dtde);
}
void dropTargetDrag(DropTargetDragEvent dtde) {
dtde.acceptDrag(dtde.getDropAction());
}
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
try {
Object source = dtde.getTransferable().getTransferData(
dataflavor[0]);
Object target = dtde.getSource();
Component component = ((DragSourceContext) source).getComponent();
Container oldContainer = component.getParent();
Container newContainer = (Container) ((DropTarget) target)
.getComponent();
newContainer.add(component);
oldContainer.validate();
oldContainer.repaint();
newContainer.validate();
newContainer.repaint();
} catch (Exception ex) {
ex.printStackTrace();
}
dtde.dropComplete(true);
}
public static void main(String[] arg) {
Button button = new Button("Drag Button");
Frame sourceFrame = new Frame("Source Frame");
sourceFrame.setLayout(new FlowLayout());
sourceFrame.add(button);
Frame targetFrame = new Frame("Target Frame");
targetFrame.setLayout(new FlowLayout());
sourceFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
targetFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
GetDropActionExample DragSourceExample = new GetDropActionExample();
DragSource dragSource = new DragSource();
DropTarget sourceDropTarget = new DropTarget(sourceFrame,
DnDConstants.ACTION_MOVE, DragSourceExample);
DropTarget targetDropTarget = new DropTarget(targetFrame,
DnDConstants.ACTION_MOVE, DragSourceExample);
DragGestureRecognizer draggesturerecognizer = dragSource
.createDefaultDragGestureRecognizer(button,
DnDConstants.ACTION_MOVE, DragSourceExample);
sourceFrame.setBounds(100, 200, 100, 60);
targetFrame.setBounds(220, 200, 100, 60);
sourceFrame.setVisible(true);
targetFrame.setVisible(true);
}
} |
|
|