import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class DragDrop 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) {
Object source = ev.getSource();
if(source instanceof DropTarget)
{
Component c = ((DropTarget)source).getComponent();
c.setBackground(Color.RED);
}
}
public void dragExit(DropTargetEvent ev) {
Object source = ev.getSource();
if(source instanceof DropTarget)
{
Component c = ((DropTarget)source).getComponent();
String name = c.getName();
String compare = "BLUE";
if(name.equals(compare))
{ c.setBackground(Color.BLUE);}
else{c.setBackground(Color.GREEN);}
}
}
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("unused")
public static void main(String[] arg) {
JFrame source = new JFrame("Source Frame");
source.setLayout(new FlowLayout());
JLayeredPane layer = new JLayeredPane();
layer.setPreferredSize(new Dimension(600,600));
JPanel p1 = new JPanel();
p1.setBackground(Color.BLUE);
p1.setName("BLUE");
p1.setBounds(0,0,600,200);
JPanel p2 = new JPanel();
p2.setBackground(Color.GREEN);
p2.setName("Green");
p2.setPreferredSize(new Dimension(600,200));
p2.setBounds(0,220,600,200);
JLabel dragAble = new JLabel();
dragAble.setBackground(Color.YELLOW);
dragAble.setOpaque(true);
dragAble.setPreferredSize(new Dimension(50,50));
dragAble.setBounds(0,440,50,50);
layer.add(p1, JLayeredPane.DEFAULT_LAYER);
layer.add(p2, JLayeredPane.DEFAULT_LAYER);
layer.add(dragAble, JLayeredPane.DRAG_LAYER);
source.add(layer);
DragDrop dndListener = new DragDrop();
DragSource dragSource = new DragSource();
DropTarget dropTarget1 = new DropTarget(p1, DnDConstants.ACTION_MOVE,
dndListener);
DropTarget dropTarget2 = new DropTarget(p2, DnDConstants.ACTION_MOVE,
dndListener);
DragGestureRecognizer dragRecognizer5 = dragSource.
createDefaultDragGestureRecognizer(dragAble, DnDConstants.ACTION_MOVE, dndListener);
source.setBounds(200, 200, 600,600);
source.setVisible(true);
/* Button button = new Button("Drag this button");
Label label = new Label("Drag this label");
Checkbox checkbox = new Checkbox("Drag this check box");
CheckboxGroup radiobutton = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("Drag this check box", radiobutton, false);
Choice country = new Choice();
// adding possible choices
country.add("India");
country.add("US");
country.add("Australia");
Frame source = new Frame("Source Frame");
source.setLayout(new FlowLayout());
source.add(button);
source.add(label);
source.add(checkbox);
source.add(checkbox1);
source.add(country);
Frame target = new Frame("Target Frame");
target.setLayout(new FlowLayout());
DragDrop dndListener = new DragDrop();
DragSource dragSource = new DragSource();
DropTarget dropTarget1 = new DropTarget(source, DnDConstants.ACTION_MOVE,
dndListener);
DropTarget dropTarget2 = new DropTarget(target, DnDConstants.ACTION_MOVE,
dndListener);
DragGestureRecognizer dragRecognizer1 = dragSource.
createDefaultDragGestureRecognizer(button, DnDConstants.ACTION_MOVE, dndListener);
DragGestureRecognizer dragRecognizer2 = dragSource.
createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_MOVE, dndListener);
DragGestureRecognizer dragRecognizer3 = dragSource.
createDefaultDragGestureRecognizer(checkbox, DnDConstants.ACTION_MOVE, dndListener);
DragGestureRecognizer dragRecognizer4 = dragSource.
createDefaultDragGestureRecognizer(checkbox1, DnDConstants.ACTION_MOVE, dndListener);
DragGestureRecognizer dragRecognizer5 = dragSource.
createDefaultDragGestureRecognizer(country, DnDConstants.ACTION_MOVE, dndListener);
source.setBounds(0, 200, 200, 200);
target.setBounds(220, 200, 200, 200);
source.setVisible(true);
target.setVisible(true);*/
}
}