Article View: borland.public.jbuilder.java.swing
Article #11377Re: [SWING] JTree and Drag'n Drop
From: mslyman
Date: Fri, 04 Jun 1999 00:00
Date: Fri, 04 Jun 1999 00:00
193 lines
6733 bytes
6733 bytes
Hi,
You could try looking into using the TreeModel. It has a method
removeNodeFromParent(Node child).
So
TreeModel treeModel = jTree.getModel();
treeModel.removeNodeFromParent(node)
Hope this works,
Mark.
In article <37565680.14EBD220@nupis.de>,
Michael =?iso-8859-1?Q?H=E4rtig?= <haertig@nupis.de> wrote:
> 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,drag
SourceListener);
> }
> }
> }
> //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,d
ropTargetDropEvent.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 ( )
> ( ) (_)
> (_)
>
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Technical Details
Message-ID:
<7j8onj$ekt$1@nnrp1.deja.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37565680.14EBD220@nupis.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,2a9d92b555df190c X-Google-Attributes: gid111d45,public From: mslyman@my-deja.com Subject: Re: [SWING] JTree and Drag'n Drop Date: 1999/06/04 Message-ID: <7j8onj$ekt$1@nnrp1.deja.com> X-Deja-AN: 485669786 References: <37565680.14EBD220@nupis.de> X-Http-Proxy: 1.0 x40.deja.com:80 (Squid/1.1.22) for client 193.120.95.3 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Fri Jun 04 14:44:07 1999 GMT Newsgroups: borland.public.jbuilder.java.swing X-Http-User-Agent: Mozilla/4.05 [en] (Win95; I)