Article View: borland.public.jbuilder.java.swing
Article #11366[SWING] JTree and Drag'n Drop
From: Michael Härtig
Date: Thu, 03 Jun 1999 00:00
Date: Thu, 03 Jun 1999 00:00
163 lines
5952 bytes
5952 bytes
Hi all,
I try to make a tree. The nodes should drag and drop in the same tree. I
created a Transferable that contains a Node as userObject. The tree is
DragSource and DropTarget. If the drop event occurs I get the
Transferable and the included node. I try to add the node to the target
node -> it works. I try to remove the node from the old position ->
nothing. If I make a System.out.println with the dragged node and the
dropped node I get different outputs. This is the output from the
userObject in the node (I think). I get "FMObject@26dbec" and
"FMObject@52fc36". But the methods (MyObject.getValue()) get the same
values.
Does anybody have done this before? Any code example?
Some of my code:
public class NupNode extends DefaultMutableTreeNode{
private FMObjekt fmObjekt = new FMObjekt(); //Only a sublass from
Object
public NupNode(){
super();
this.fmObjekt = new FMObjekt();
}
public NupNode(FMObjekt fmObjekt){
super(fmObjekt);
this.fmObjekt = fmObjekt;
}
public FMObjekt getFMObjekt(){
return fmObjekt;
}
}
public class NupTree extends JTree implements DragGestureListener,
DropTargetListener {
DragSource dragSource = DragSource.getDefaultDragSource();
DropTarget dropTarget;
final static DragSourceListener dragSourceListener = new
MyDragSourceListener();
static class MyDragSourceListener implements DragSourceListener {
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {}
public void dragExit(DragSourceEvent DragSourceEvent) {}
public void dragOver(DragSourceDragEvent DragSourceDragEvent) {}
public void dropActionChanged(DragSourceDragEvent
DragSourceDragEvent) {}
}
public NupTree () {
dragSource.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_COPY_OR_MOVE, this);
dropTarget = new DropTarget (this, this);
}
// DragGestureListener
public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
TreePath path = getSelectionPath();
if (path == null) {
System.out.println ("Nothing!");
}
else {
NupNode node = (NupNode)path.getLastPathComponent();
if (node.isLeaf()){
TransferableElement tr = new TransferableElement(node);
System.out.println("send "+node); //out: "FMObject@26dbec"
System.out.println("send "+node.getFMObjekt().getFmNummer());
//out: "1"
dragSource.startDrag(dragGestureEvent,DragSource.DefaultCopyDrop,tr,dragSourceListener);
}
}
}
//DropTargetListener
public void dragEnter (DropTargetDragEvent dropTargetDragEvent) {
dropTargetDragEvent.acceptDrag (DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit (DropTargetEvent dropTargetEvent) {}
public void dragOver (DropTargetDragEvent dropTargetDragEvent) {}
public void dropActionChanged (DropTargetDragEvent
dropTargetDragEvent) {}
public synchronized void drop (DropTargetDropEvent
dropTargetDropEvent) {
try {
Transferable tr = dropTargetDropEvent.getTransferable();
if (tr.isDataFlavorSupported(TransferableElement.NODE_FLAVOR)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
NupNode node =
(NupNode)tr.getTransferData(TransferableElement.NODE_FLAVOR);
System.out.println("get "+node); //out: "FMObject@52fc36"
System.out.println("get "+node.getFMObjekt().getFmNummer());
//out: "1"
NupNode parentNode =
(NupNode)((this.getPathForLocation(dropTargetDropEvent.getLocation().x,dropTargetDropEvent.getLocation().y)).getLastPathComponent());
parentNode.add(node); //this works
node.removeFromParent();//does nothing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
}
}
catch (Exception e) {
e.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
}
public class TransferableElement implements Transferable{
private NupNode node;
final public static DataFlavor NODE_FLAVOR = new
DataFlavor(NupNode.class,"Nup Node");
static DataFlavor flavors[] = {NODE_FLAVOR};
public TransferableElement(NupNode nupNode){
this.node = nupNode;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean returnValue = false;
for (int i=0 ; i<flavors.length; i++) {
if (flavor.equals(flavors[i])) {
returnValue = true;
break;
}
}
return returnValue;
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public Object getTransferData(DataFlavor flavor) throws
UnsupportedFlavorException {
return node;
}
}
Bye Michael
--
////
(o o)
+-----ooO--(_)--Ooo---------------------------------------+
| |
| Michael H�rtig Neumann & Partner |
| Informationssysteme GmbH |
| Softwareentwicklung An der Hohen Stra�e 1 |
| 08393 Meerane |
| |
+------------Oooo-----------------------------------------+
oooO ( )
( ) (_)
(_)
Technical Details
Message-ID:
<37565680.14EBD220@nupis.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,2a9d92b555df190c,start X-Google-Attributes: gid111d45,public From: Michael Härtig <haertig@nupis.de> Subject: [SWING] JTree and Drag'n Drop Date: 1999/06/03 Message-ID: <37565680.14EBD220@nupis.de> X-Deja-AN: 485211061 Content-Transfer-Encoding: 8bit Organization: Another Netscape Collabra Server User X-Accept-Language: de Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing