How can I keep original components after drag and drop?

Dear Friends:

I have following code to do Drag and Drop,

I can successfully Drag and Drop DragLabel into JButton, but after DnD, therer is no DragLabel in original place, but I hope even I Drag and Drop DragLabel into JButton, there is still DragLabel at original place, how to do it?

Thanks a lot!!

import java.awt.*;

import java.awt.datatransfer.*;

import java.awt.dnd.*;

import java.awt.event.*;

import java.io.*;

import java.awt.*;

import java.awt.dnd.DnDConstants;

import java.awt.dnd.DragGestureEvent;

import java.awt.dnd.DragGestureListener;

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 java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.datatransfer.*;

import javax.swing.border.BevelBorder;

publicclass AAADNDPanel0extends JPanelimplements DragGestureListener, DragSourceListener,

DropTargetListener, Transferable

{

staticfinal 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;

elsereturnnull;

}

public DataFlavor[] getTransferDataFlavors(){return supportedFlavors;}

publicboolean isDataFlavorSupported(DataFlavor flavor){return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType);}

// DragGestureListener method.

publicvoid dragGestureRecognized(DragGestureEvent ev)

{

ev.startDrag(null, this,this);

}

// DragSourceListener methods.

publicvoid dragDropEnd(DragSourceDropEvent ev){}

publicvoid dragEnter(DragSourceDragEvent ev){}

publicvoid dragExit(DragSourceEvent ev){}

publicvoid dragOver(DragSourceDragEvent ev){ object = ev.getSource();}

publicvoid dropActionChanged(DragSourceDragEvent ev){}

// DropTargetListener methods.

publicvoid dragEnter(DropTargetDragEvent ev){}

publicvoid dragExit(DropTargetEvent ev){}

publicvoid dragOver(DropTargetDragEvent ev){ dropTargetDrag(ev);}

publicvoid dropActionChanged(DropTargetDragEvent ev){ dropTargetDrag(ev);}

void dropTargetDrag(DropTargetDragEvent ev){ ev.acceptDrag(ev.getDropAction());}

publicvoid 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();

//#######################################################################################

System.out.println("component =" + component.getClass().getName());

//#######################################################################################

}

catch (Exception ex){ ex.printStackTrace();}

ev.dropComplete(true);

}

public AAADNDPanel0()

{

Button button =new Button("Drag this button");

Label label =new Label("Drag this label");

Checkbox checkbox =new Checkbox("Drag this check box");

JPanel source =new JPanel();

source.setLayout(new FlowLayout());

source.add(button);

source.add(label);

source.setPreferredSize(new Dimension(300,600));

JPanel target =new JPanel();

target.setLayout(new FlowLayout());

target.add(checkbox);

target.setPreferredSize(new Dimension(300,600));

final JSplitPane splitPane =new JSplitPane();

splitPane.setLeftComponent(target);

splitPane.setRightComponent(source);

DragSource dragSource =new DragSource();

DropTarget dropTarget1 =new DropTarget(source, DnDConstants.ACTION_MOVE,this);

DropTarget dropTarget2 =new DropTarget(target, DnDConstants.ACTION_MOVE,this);

DragGestureRecognizer dragRecognizer1 = dragSource.createDefaultDragGestureRecognizer(button, DnDConstants.ACTION_MOVE,this);

DragGestureRecognizer dragRecognizer2 = dragSource.createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_MOVE,this);

DragGestureRecognizer dragRecognizer3 = dragSource.createDefaultDragGestureRecognizer(checkbox, DnDConstants.ACTION_MOVE,this);

source.setBounds(0, 200, 200, 200);

target.setBounds(220, 200, 200, 200);

add(splitPane, BorderLayout.CENTER);

setPreferredSize(new Dimension(600,600));

}

publicstaticvoid main(String[] args){

AAADNDPanel0 dm =new AAADNDPanel0();

JFrame frame =new JFrame();

frame.getContentPane().add(dm, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

}

Can any guru ggive me help?

Thanks

Have a good day.

sunny

[10860 byte] By [sunnymanmana] at [2007-11-26 23:46:37]
# 1
Please stop re-posting this same question multiple times. You have received numerous replies to the previous 5 or 6 posts of the question.If you must post about this question, add to one of the other posts.
ChuckBinga at 2007-7-11 15:20:10 > top of Java-index,Desktop,Core GUI APIs...
# 2
thanks for advice, but this code I change already, it is totally different program, so far I did not get any feed back from this program, subject is same, but code is different, you can check
sunnymanmana at 2007-7-11 15:20:10 > top of Java-index,Desktop,Core GUI APIs...
# 3
So still keep it all in one thread.
ChuckBinga at 2007-7-11 15:20:10 > top of Java-index,Desktop,Core GUI APIs...