listModel.addElement(new ImageIcon(directory + "\\" + files[i].getName()));
}
}
}class ImageSelection implements Transferable, Serializable {
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] supported = new DataFlavor[1];
supported[0] = new DataFlavor(getClass(),"SelectedImage");
System.out.println(supported);
return supported;
}
public Object getTransferData(DataFlavor f)
throws UnsupportedFlavorException {
return this;
}
public boolean isDataFlavorSupported(DataFlavor aFlavor)
{
return aFlavor.equals(new DataFlavor(getClass(),"SelectedImage"));
}
}
class DragContainer extends JList implements DragGestureListener, DragSourceListener {
private DragSource iDragSource = null;
DataFlavor DATA_FLAVOR;
public DragContainer(ListModel lm) {
super(lm);
try {
DATA_FLAVOR = new DataFlavor(ImageIcon.class,"SelectedImage");
}
catch (Exception e) {
System.out.println("Error in dataflavor for dragcontainer");
}
iDragSource = new DragSource();
iDragSource.createDefaultDragGestureRecognizer(this,DnDConstants.ACTION_MOVE, this);
}
public void dragGestureRecognized(DragGestureEvent aEvt) {
ImageIcon imageSelected = (ImageIcon) getSelectedValue();
ImageSelection imsel = new ImageSelection();
if (imageSelected != null) {
System.out.println("startdrag...");
iDragSource.startDrag(aEvt,DragSource.DefaultCopyNoDrop,imsel,this);
}
else
System.out.println("Nothing Selected");
}
public void dropActionChanged(DropTargetDragEvent event) {
}
public void dropActionChanged(DragSourceDragEvent event) {
}
public void dragDropEnd(DragSourceDropEvent event) {
}
public void dragEnter(DragSourceDragEvent event) {
}
public void dragExit (DragSourceEvent event) {
}
public void dragOver(DragSourceDragEvent event) {
DragSourceContext context = event.getDragSourceContext();
context.setCursor(null);
context.setCursor(DragSource.DefaultCopyDrop);
}
}
class DropContainer extends JPanel implements DropTargetListener {
private DropTarget iDropTarget = null;
public DropContainer() {
iDropTarget = new DropTarget(this,this);
setBackground(Color.red);
}
public void dragEnter (DropTargetDragEvent event) {
System.out.println("dragenter");
event.acceptDrag(DnDConstants.ACTION_MOVE);
}
public void dragExit(DropTargetEvent event) {
System.out.println("dragexit");
}
public void dragOver (DropTargetDragEvent event) {
System.out.println("dragover");
event.acceptDrag(DnDConstants.ACTION_COPY);
}
public void dropActionChanged (DropTargetDragEvent event) {
System.out.println("dropactionchanged");
event.acceptDrag(DnDConstants.ACTION_COPY);
}
public void drop(DropTargetDropEvent aEvt) {
System.out.println("drop");
try {
Transferable transferable = aEvt.getTransferable();
DataFlavor aliFlavor = new DataFlavor(getClass(),"Selected Image");
System.out.println(aliFlavor);
if (transferable.isDataFlavorSupported(aliFlavor)) {
aEvt.acceptDrop(DnDConstants.ACTION_MOVE);
ImageIcon icon = (ImageIcon)transferable.getTransferData(aliFlavor);
aEvt.getDropTargetContext().dropComplete(true);
}
else {
System.out.println("rejecting drop");
aEvt.rejectDrop();
aEvt.getDropTargetContext().dropComplete(false);
}
}
catch (Exception exc) {
exc.printStackTrace();
aEvt.rejectDrop();
aEvt.getDropTargetContext().dropComplete(false);
}
}
}//class DropContainer
Replies: