Drag and Drop nodes in JTree

Is there a way to drap and drop JTree nodes without resorting to hacks? http://www.javaworld.com/javatips/jw-javatip97.html outlines a hack to get drag and drop working but it is kinda old. I am wondering whether there is a now a way to do this in Swing. http://weblogs.java.net/blog/shan_man/archive/2006/01/first_class_dra.html outlines a seemingly promising way but when I tried it it still wouldn't let me drag JTree nodes. (btw, if you are going to try it, you must replace TransferHandler.TransferInfo with TransferHandler.TransferSupport. It has apprently been renamed.)

I have implemented drag and drop of nodes in JTree they same way it is explained in JavaWorld link that you mentioned, I customized it for my application and it worked great.

Similar Messages

  • Drag and Drop between two JTrees (urgent)

    Hello,
    I want to drag a node from one JTree and Drop it to another JTree, but it is not working, I am able to Drag from JTree and drop that on Label successfully. I didn't get any tutorial regarding my problem. If anyone of you have made it. Can you please show me your code.
    Thanks in advance.
    jStudent3

    Can you please pass on the code for dragging and dropping node between two JTree? I would be glad if u cud help me. Plz mail the code to [email protected]
    thanks
    adithya

  • How to drag and drop nodes in Tree?

    Hi,
    I want to drag and drop nodes in the tree. For example a tree represents the hierarchy of employees reporting in an organization by using tree.I want to change the reporting an employee visible in the tree by simple drag and drop operation in place of going to another form for updating each employee record indiviually.
    Regards
    Piyush

    Ron,
    I looked into implementing drag / drop in one of the apex trees I created today and ran across this thread. Thank you Ron for the links, it helped a lot.
    I added the code below to my page's "Execute when Page Loads" (tree region id is "tree_reg") and the tree is now drag/drop enabled.
    It did break the [+] icon from collapsing the tree though ... but the apex.widget.tree buttons still work
    var regTree = apex.jQuery("#tree_reg").find("div.tree");
    regTree.tree({ 
    callback : {
    onmove: function(NODE, TREE_OBJ, REF_NODE, TYPE)
    {alert(NODE.id+"   "+TREE_OBJ.id+"   "+ REF_NODE);}
    });Next, I plan on creating a AJAX call using NODE.id, TREE_OBJ.id, and REF_NODE
    V/R
    Ricker

  • Problem with Drag and Drop on a JTree

    I would like to have a JTree with drag and drop support with the following characteristics:
    1) The JTree holds near 100 different kinds of TreeNodes
    2) The drag and drop operation uses TreeNodes as Transferable
    3) The drop operation adds a new TreeNode child on the TreeNode that accepts the drop
    4) There are some rules for a TreeNode to accept the drops by example:
         Type1 can accept Type2
         Type2 can accept Type3
         All other combinations are not allowed
    I am using the following classes to support this behavior
    JTree
    DataFlavor
    DragSourceListener
    DropTarget
    DropTargeListener
    DragSource
    GestureListener
    Transferable
    The question is:
    How to change the cursor shape when the mouse is over a node to show graphically the drop accepting rules.?
    I can not use the the following methods to change the cursor-shape
    DropTargetListener.dragEnter
    And
    DropTargetListener.dragOver
    Case they receive a DropTargetDragEvent witch don�t knows nothing about the TreeNode type dragged.
    I can not create a TreeNode on the method GestureListener.dragGestureRecognized cause this could not work when dragging between two diffents JVM.

    Hello,
    You might want to search this forum for this class : DNDTree.java. It pretty much does the job.

  • Drag and Drop between two JTrees

    Hello,
    I want to drag a node from one JTree and Drop it to another JTree, but it is not working, I am able to Drag from JTree and drop that on Label successfully. I didn't get any tutorial regarding my problem. If anyone of you have made it. Can you please show me your code.
    Thanks in advance.
    jStudent3

    At last i got an article on this and it is working fine.
    Go to http://www.javaworld.com/javatips/jw-javatip97_p.html

  • Drag and Drop Problem in JTree - Application hangs

    Dear Friends,
    My application hangs when I try to pop a JOptionPane, asking user to select a option. I can't do anything as whole CPU memory is consumed by this. If I don't pop option then there is no problem at all. Everything works fine.
    Dialog is poped when user makes a drop on a node.
    OS : WIN NT 4.0 SP 6
    WEB SERVER : TOMCAT 3.2.2
    Thanx in advance

    Not sure if this will work inside classes, but in working with AS3, this would issue the command to object you just clicked, and the object you just dropped:
    // This function is called when the mouse button is pressed.
       function startDragging(event:MouseEvent):void
            event.currentTarget.startDrag();
        // This function is called when the mouse button is released.
        function stopDragging(event:MouseEvent):void
           event.currentTarget.stopDrag();

  • JTree Drag and Drop (Multi Nodes)

    Hi All!
    I have been searching the forums for a few hours over the last few days and have found some nice examples and explanations on how to implement drag and drop for a jtree.
    However, I would love to know how this is accomplished when selecting multiple nodes.
    I want to be able to see all the selected nodes being dragged...
    Anyone implemented anything of the sort... or even just knows how to do this?
    Thanks in advance!!
    Aaron.

    When you drag get the [x,y] mouse position and use the JTree method:
    getRowForLocation(x, y) for node you at the also for the parent node location of the child node.
    Once the childY is less than childParentY, you stop the drag.
    Hope that helps?

  • Drag and Drop and java.io.NotSerializableException

    I have been implementing some code to essentially allow me to drag and drop from one JTree to another. The requirement was to be able to drag multiple nodes at one time and drop them on the other JTree in the order selected.
    I was getting a java.io.NotSerializableException when I was in the drop code in the destination jTree and could not figure out what was going on. Basically, I was wrapping all the objects that i wanted to transfer in a custom ArrayList subclass that implemented transferable. The NotSerializableException was was indicating that the object in the JList was not serializable.
    If you are getting a NotSerializableException and you have defined a custom DataFlavor like so:
    public static DataFlavor DATATYPE_TRANSFER =
            new DataFlavor(
               TransferableImplementor.class,  //  make sure that this is the class that implements transferable
                "Datatype Information");make sure that the .class is indeed the class that implements the transferable interface (and not some other random .class). You can be led down a wild goose chase on trying to make sure every object in your Transferable implementor class is serializable, when the problem lays in the DataFlavor instantiation.
    Nik

    If you haven't already read these, here are a couple of good sources for DND tree implementation details.
    http://www.javaworld.com/javaworld/javatips/jw-javatip97.html
    http://www.javaworld.com/javaworld/javatips/jw-javatip114.html

  • Drag and drop problem from wizard inside another drop

    I am using JDK 1.5.0_11 Swing.
    I am facing a Drag and Drop problem which is described below:
    A drag and drop operation invokes a wizard, from which another wizard is invoked. Within one of the panels of this 2nd wizard, I am trying to do a drag and drop from a JTextarea to a JTree node, but am NOT able to. I am not getting any exceptions. I have written a transfer handler for the JTree node to handle the text transfers from a JTextArea. When I try to do a drag and drop from a JTree node to another JTree node I'm getting the 'InvalidDNDOperation: drag and drop in progress' exception.
    When the 2nd wizard is invoked directly, I am able to do the drag and drop from JTextArea to a JTree node and also from a JTree node to another JTree node.
    I tried spawning a new thread for the 2nd wizard, but am still unable to initiate drag and drop.
    Should I be spawning a new thread for the 1st wizard?
    Message was edited by:
    sbandyop

    Please post Swing questions in the Swing forum: http://forum.java.sun.com/forum.jspa?forumID=57

  • How do drag and drop in htree?

    i would like to drag and drop node in tree look like Object Navigator in
    Oracle Form Developer 6i.

    Hi jimbo7,
    Is this through the Google Calendar web interface, or are you subscribed to the calendar in the Calendar application?
    If it is the webpage, I have recently released an application that may help.  Multi Event Filter is designed to take dates as text from any source (text of email, CSV or spreadsheet, webpage, text file) and quickly and easily creates events in Calendar. The application is $2.99 to buy on Apple's Mac App Store.  You can see more information about it on my website.
    On the other hand, if you are subscribing to the calendar in Calendar you should be able to copy the event in Calendar, change the active calendar to the one you want the event in and paste.
    Best wishes
    John M
    As I sell software on my site and ask for donations, the Apple Discussions Use Agreement requires that I state that I may receive some form of compensation, financial or otherwise, from my recommendation or link.

  • Multiple Item Drag and Drop

    I'm working on an application where I need multiple item DnD between JTrees and JTables. I'm using Java 1.4.1
    I have the basics of multiple item DnD working, by creating Transferable classes that contain a collection of items.
    The problem I have is when I drag a multiple selection within a JTree (i.e. DragSource and DropTarget are the same) then my call to event.getCurrentDataFlavors() in my dragOver(DropTargetDragEvent event) method is not returning any data flavours unless I drag over to another JTree. If I now drag back over to the source JTree (so that DragSource and DropTarget are again the same) as if by magic I now get DataFlavours whilst dragging over the source container.
    This is driving me nuts! :-)
    Has anybody else seen this and do they have a solution?

    I have the same problem here.
    When I try to perform drag and drop in a JTree (DragSource and DropTarget are the same), calling event.getCurrentDataFlavors() in the dragOver(DropTargetDragEvent event) method in the DropTargetListener returns me nothing!!
    However, the call of event.getCurrentDataFlavors() in the
    drop(DropTargetDropEvent event) does return me the correct
    information.
    On the doc, the DropTargetDragEvent.getCurrentDataFlavors()
    should mimic DropTargetDropEvent.getCurrentDataFlavors().
    So, I suppose both functions should work in the same way
    properly.
    Is there anything wrong with my understanding or is that a bug
    of the Java dnd?

  • Drag and Drop different nodes within a JTree

    I have a JTree that represents a hierarchical object. Within the tree are different nodes representing different objects, each within a container,
    For Example:
    A
    -B container
    -- B NodeObject
    -- B NodeObject
    -C Container
    -- C NodeObject
    -- C NodeObject
    I have got the drag and drop working but as the gesture listener is on the actual JTree object it allows me to drag any object to any other place.
    What i want to be able to do is to force the drag and drop to allow only objects of type C to be put in the C container, etc and to reject any moves
    over other containers.
    As TreeNodes are not Components how do i force this behaviour?
    If i have to i can force the behaviour after the mouse has been released but I would prefer it if Swing would show the correct mouse over icons
    when I put an object over the wrong container.
    Thanks
    Dan Alford

    When you drag get the [x,y] mouse position and use the JTree method:
    getRowForLocation(x, y) for node you at the also for the parent node location of the child node.
    Once the childY is less than childParentY, you stop the drag.
    Hope that helps?

  • Display JTree Node as image  while Drag And Drop

    HI all.
    I have multiple 5 JTrees which involve drag and drop to each other . I would like to display the node(JCompenent) as image/cursor which a DnD operation .So that user could see what he drags .
    I tried out the example about ghost image .
    http://www.javaworld.com/javaworld/javatips/jw-javatip114.html
    This works fine ,but when i apply the same concepts to my application it doesnt work .
    Could any one suggest what could be problem.
    Cheers,Baiju

    I also need to drag my node on my scene during a "drag and drop" operation. But i can't find a solution.
    I've tried to use "startFullDrag()" and catch MouseDragEvent, but nothing ...
    Can anyone help me ?
    Thank !

  • Drag and Drop Jtree

    hi
    this code in a jtree accepts stuff dropped over using drag and drop, but when it accepts drops, they register fine. but when i expand the node, and then drop on it it refuses to accept the drop, the drop just doesnt happen!
    can you help? im totaly stuck!
    i can see that the drop is happening from system.out.println but its not regiistering on the tree gui even when i repeatedly collapse/expand the node!
    once i touch a node it refuses to accept any drops henceforth!
    public void drop(DropTargetDropEvent dtde) {
              Point point = dtde.getLocation();
              int x = (int)point.getX();
              int y = (int)point.getY();
              numberDropped++;
              TreePath path = getPathForLocation(x,y);
              System.out.println(path);
              try {
                   selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
                   System.out.println(selectedNode.getUserObject());
                   for(int i=0;i<selectedNode.getChildCount();i++) {
                        System.out.println(((DefaultMutableTreeNode)selectedNode.getChildAt(i)).getUserObject());
              } catch(Exception e) {
                   System.out.println("No available node");
              System.out.println(selectedNode.getAllowsChildren());
              System.out.println(selectedNode.getChildCount());
              try {
                   DataFlavor stringFlavor = DataFlavor.stringFlavor;
                   Transferable tr = dtde.getTransferable();
                   if(dtde.isDataFlavorSupported(stringFlavor)) {
                        String filename = (String) tr.getTransferData(stringFlavor);
                        try {
                             URL picture = new URL(filename);
                             addImageToNode(selectedNode, picture);
                             System.out.println(filename + selectedNode);
                        }catch(Exception e){
                             e.printStackTrace();
                        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                        dtde.dropComplete(true);
                   else {
                        dtde.rejectDrop();
                        System.out.println("Drop rejected");
              catch(UnsupportedFlavorException ufe) {
                   ufe.printStackTrace();
              catch(IOException ioe) {
                   ioe.printStackTrace();
         }this is the addImageTiNode method
    public void addImageToNode(DefaultMutableTreeNode givenNode, URL pictureLocation) {
              System.out.println("In aITN");
              System.out.println(givenNode.getUserObject());
              if(givenNode.getAllowsChildren()) {
                   DefaultMutableTreeNode image = new DefaultMutableTreeNode(pictureLocation, false);
                   givenNode.add(image);
                   this.repaint();
              else {
                   System.out.println("This node does not allow children");
              //System.out.println(selectedNode + " at " + pictureLocation);
         }

    You need to call DropTargetDragEvent.rejectDrag() in
    the dragOver() method of DropTargetListener.Thanks. I have done that but it doesn't change the cursor to the defaultNoMove cursor. Any other suggestions?
    Tiny

  • Drag and Drop in JTree

    Hello
    i have developped sucessfully the drag on drop operation in my JTree and it works well but now i will also change the cursor of the mouse.
    so if you drag and drop an object in a jtree you must view the movement of that object, i think that it may be possible to change the cursor of the mouse to do that but i don't know how it works
    Is it also possible that the entire structure under the moved node is visible during moving?
    I hope that somebody can help me
    Jeff R.

    here is the code for the drag and drop operation
    public class TreeListener implements TreeSelectionListener,MouseListener,                               DragGestureListener, DropTargetListener,DragSourceListener{
         private CatalogManagerFrameListener frame;
         private CatalogElement selectedElement;
         /** Variables needed for DnD */
         DragSource dragSource = DragSource.getDefaultDragSource() ;
         private TreePath selectedTreePath;
         /** DragGestureListener interface method */
         public void dragGestureRecognized(DragGestureEvent e) {
         JTree tree = (JTree)e.getComponent();
         //Get the selected node
         if (selectedElement != null) {
              //Get the Transferable Object
              Transferable transferable = (Transferable) selectedElement;
              //Select the appropriate cursor;
              Cursor cursor = DragSource.DefaultMoveDrop;
              tree.setCursor(cursor);
              //begin the drag
              dragSource.startDrag(e, cursor, transferable, this);
         /** DropTargetListener interface method - What we do when drag is released */
         public void drop(DropTargetDropEvent e) {
              JTree tree = (JTree)((DropTarget)e.getSource()).getComponent();
              try {
                   Transferable tr = e.getTransferable();
                   //flavor not supported, reject drop
                   if (!tr.isDataFlavorSupported(CatalogElement.INFO_FLAVOR)){
                        e.rejectDrop();
                   //get source
                   CatalogElement source = selectedElement;
                   //get new parent node
                   Point loc = e.getLocation();
                   TreePath destinationPath = tree.getPathForLocation(loc.x, loc.y);
                   CatalogElement destination = (CatalogElement) destinationPath.getLastPathComponent();
                   if (testDropTarget(destinationPath, selectedTreePath)) {
                        move(source,destination);          
                        destinationPath.pathByAddingChild(source);
                        tree.setSelectionPath(destinationPath);
                   else{
                        e.rejectDrop();
              catch(Exception ex){     
                   e.rejectDrop();

Maybe you are looking for

  • Usb powered on while pc is off on x58 pro

    Noob question here, i want my pc to be able to power my usb ports while the power is off so my mp3 player can charge. I managed to do it the other day and now its not working again. >_< its something i changed in the bios i think. Anybody know what o

  • Can I buy an iTunes gift certificate for someone outside the U.S.?

    can I buy an iTunes gift certificate for someone outside the U.S.?

  • Windows 7 (64bit) and NI-Visa 5.2

    Hello, i am using Windows 7 64 bit and just installed TS/LV2012 and NI-Visa 5.2 Then i started MAX and want to add a new TCPIP-instrument but it says: "Either NI-VISA is not installed on this system or NI-VISA does not support creating new resources

  • Handling 255 Characters in BW

    There is a strange requirement from the user that what ever the notes the Sales rep makes attending the sales call has to be shown in BW as well.This is like the CSR making notes while you are talking to him/her on the phone. In the source system, th

  • Oracle HRMS training course form Oracle University

    iWare Logic provides "Oracle Apps Training" and Certification in Oracle crm in association with Oracle University. Oracle HRMS Training and Certification topics eBusiness Suite Essentials for Implementers Ed 4 Enterprise Management Fundamentals: Work