Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Drag and Drop in JTrees

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Drag and Drop in JTrees

    Is there a good way to track drags with a mouse listener? Or does anyone here understand how dnd (drag-and-drop) works with JTree? I'm stuck on converting a DefaultMutableTreeNode into a MIME string that I can use with DataFlavor. Ideally, I'd like to be able to have the dnd be done in my application rather than using JTree's (for undo/redo implemented by my application), but I've read from quite a few sources that doing so now is impractical, and it's best to implement a TransferHandler.

    I'm using a basic JTree object with:

    this.tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    this.tree.setAutoscrolls(true);
    this.tree.setDragEnabled(true);
    this.tree.setDropMode(DropMode.ON_OR_INSERT);
    this.setTransferHandler(new TestTransferHandler());

    It also has a bunch of nodes that all extend from DefaultMutableTreeNode. It is possible to select multiple nodes that are not next to each other. The TestTransferHandler class is really nothing more than a class that extends TransferHandler (I haven't figured out which methods I need to override, and with what). Also, do I need to have my custom node objects implement Transferable? If so, how would/should I go about this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Drag and Drop in JTrees

    Drag and Drop is always a pain in the you know what. Its been a while since I last played with dnd in a JTree, but its similar to dnd in other Components. One needs to implement and set the TransferHandler (createTranferable is one method that should be overridden) and implement/register a DropTargetListener. In the drop method of DropTargetListener, you can use the mouse location from the event to see where the drop needs to go, then deal with the tree rearrangement (and on success you can deal with the undo/redo). In your case transferring multiple nodes that are not consecutive may be a bit complex depending upon your tree structure, so you may need to create a custom Transferable and DataFlavor

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Drag and Drop in JTrees

    I think I found the answer to my question... It's much more concise than the example given at Sun's website
    Java Tip 97: Add drag and drop to your JTrees. So much work, though. Oh well, I hope it's worth the trouble.

Similar Threads

  1. Struts drop down
    By kalees in forum Web Frameworks
    Replies: 1
    Last Post: January 15th, 2010, 12:56 AM
  2. 'MouseUp' Drag and Drop Events
    By copeg in forum AWT / Java Swing
    Replies: 0
    Last Post: November 10th, 2009, 07:21 PM