JTree, drag n drop, easy or hard?

I have rapidly looked at some examples and some old threads about Drag�n�drop in general and more specific for JTree�s.
A quick (maybe to quick?) conclusion is that there is a lot of code for doing this. What I want to do is to move nodes in one single JTree. Do I really need that much code or can I just implement some nice interfaces to acheive this?

I ran over a chapter, Adding Drag-and-Drop Functionality, in a book, Pro Java Programming. I think it has been very helpful for my understanding so far. Even though I haven�t produced any code yet.
At first, it is rather much you have to learn, but I think when all pieces come together, its quite straight forward.
A question: My intent is to create a drag-and-droppable JTree. Have I understood things right if I say that I need to create some kind of Flavor-thingie, that is a TreeNode (DefaultMutableTreeNode) flavor to make it work?
My own answer Yes if that is what I want to transfer between the drag and the drop.
Another question related to the first:
The following code is from an Image Viewer app where I think you shall be able to move images. Does every node need this or just the tree?
My own answer This is at least what I do at the tree. And since a node isnt a component, it is pretty obvious it is the tree.
protected void addNewComponent(Component comp, Point location) {
  DragSource source = DragSource.getDefaultDragSource();
  source.createDefaultDragGestureRecognizer(comp,
      DnDConstants.ACTION_COPY_OR_MOVE,
      new MyGestureListener());
  comp.setLocation(location);
  comp.setSize(comp.getPreferredSize());
  add(comp);
  repaint();
}Message was edited by:
sandsater

Similar Messages

  • JTree Drag N Drop

    Hello All,
    I am implementing Drag and Drop between 2 JTrees in a JTabbedPane.
    Tree 1 on tab 1 can drag nodes to Tree 2 on tab 2.
    Tree 2 can also move nodes around itself using drag and drop.
    The problem I am having is that I need to know the source of the drag such that if Tree 2 is the source and the destination the node should be cut and not copied.
    How can I determine if Tree 2 should move the dragged node or simply copy it?
    nes

    I guess you could save some identifier for the drag-source when the drag-operation occurs, like:
    private String treeNameForDrag = null;
    public void dragGestureRecognized(DragGestureEvent dge) {
    treeNameForDrag = ((JTree)dge.getSource()).getName();
    dge.startDrag(par1, par2, par3);
    and when you get the dropAction you look on the variable to see which tree that is the dragSource.You have to set the name for the trees (when you create them).
    public void drop(DropTargetDropEvent dtde) {
    if (treeNameForDrag.equals("tree2"))
    //Perform cut operation
    else
    //Perform move operation
    I hope I got your problem right, otherwise explain the problem again and I can have another look...
    Good luck!

  • 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?

  • Can't drag or drop into external hard drive anymore. Worked perfectly before.

    I bought a Seagate external hard drive about a year ago. I reformatted it the moment I took it out of the box, plugged it into my laptop, and everything worked perfectly. I dragged and dropped as I liked, and nothing went wrong with it at all. Then I decided to restore my laptop to do a bit of spring cleaning, and suddenly, I can't drag anything INTO the hard drive or open any programs either. It shows up on my desktop and Finder. And I can take things OUT of it, thankfully, but I can't put anything into it, which is extremely frustrating because I use my hard drive as storage and backup.
    I did a bit of looking around online, and people are saying that I'll need to reformat it (which I already did), but it'll erase everything in it. Because my hard drive holds almost 300 GB, I'm not too keen on doing that, because it will take AGES, and there's no confirmation that that will actually work. If you have any solutions, please help! And please go step by step as well--I'm absolutely no good with the tech language.

    1. verify same on another Mac if possible.
    2. Go to disk utility and check verify and repair on the drive and see if it checks out
    3. You never should again even consider having a single external copy of you data, at any time for any reason, HD are too cheap to endanger your data like that.
    4. Likely dead SATA bridge card, however I cant recall an instance in which communication was one way on an external with a dead SATA bridge card.
    verify 1 and 2,  and SMC reset your Mac and verify again
    SMC reset
    Shut down the computer.
    Plug in the MagSafe power adapter to a power source, connecting it to the Mac if its not already connected.
    On the built-in keyboard, press the (left side) Shift-Control-Option keys and the power button at the same time.
    Release all the keys and the power button at the same time.
    Press the power button to turn on the computer. 
    Note: The LED on the MagSafe power adapter may change states or temporarily turn off when you reset the SMC.
    Then I decided to restore my laptop to do a bit of spring cleaning,
    Define what you did in your "restore" and spring cleaning.

  • JTree drag and drop

    Hi.. How can I drag and drop items from one JTree to another??? thanks!!!

    The following link might help you :
    http://forum.java.sun.com/thread.jspa?forumID=57&threa
    dID=296255Thanks! this thread has been useful. How can I manipulate the code so that I can have two different trees instead of just one tree? How can I make the drag and drop one-way???

  • JTree Drag and Drop question

    I have drag and drop working fairly well using jdk 1.4. I'm able to copy and move objects around in the tree and change the cursor as it moves over various objects in the tree. The only problem I'm having is when the user drops something into someplace that rejects the drop, for instance in the blank area of the JTree object. In this case, the drop never seems to complete. If I try to drag that same object again it throws an InvalidDnDOperationException saying that a DnD operation is already in progress. I noticed that the drop() operation is not being called when the user drops the object into a rejected area.
    I've searched the forums and haven't seen anyone else report this problem. Does anyone have any suggestions?

    I'd love it too if you would send me you code, or post it so others can benefit; there seems to be quite a number of people who want DnD JTree functionality. I've been going nuts trying to figure out the best way to implement DnD on a JTree. I started using the new TransferHandler and had reasonable success with it. But, I'm not sure how to get the cursor to change based on DropTarget (not all nodes are created equal in this regard). I need access to the DragSourceContext, but I don't know how to get it when using the TransferHandler method.
    I have other issues with the TransferHandler also, like trying to make DnD work consistently with Cut-And-Paste all in the same TransferHandler. Or, trying to deal with the fact that you can change a move-type DnD operation into a copy-type operation after you start dragging, but before you drop just by pressing the control-key. This sort of implies to me that the original createTransferable() should always make a copy, so that the only issue is whether to clean up in exportDone().

  • How can I copy file/folders from and to hard drives? How can I have two or more windows of explorer so that I can drag and drop from one hard drive to another?

    Hi,
    I am a new customer (purchased last week) of MacBookPro (latest) and its my first ever Apple laptop.
    I am almost lost to find the operating tools.
    1. How can I copy files from hard drive to computer and vis-a-versa?
    2. Only can browse one but how can I have two or more browses at the same time like in MS windows?
    3. No right click works on track pad?
    4. Is there application/ program compatible to MS office? If so how much and how can I purchase?
    5. Is there AutoCAD program that is compatible to MS Window AutoCAD? and if yes how much and how can I purchase?
    I will very much appreciate if you could email me the with the solution/answers please.
    Regards,
    Khagendra Limbu

    1) For some reason this happens once in a while with FAT32 enabled external harddrives. I have no idea why this happens but it most easily solved by reformatting the drive. First make sure you've copied everything from the external drive to your internal harddrive. You've done this earlier so I assume you know how to do this.
    Now the reformatting part. Open Finder and go to Applications. Look for the folder called "Utilities". Click on it. In it you find a program called "Disk Utility". Click on it. Now look for the external drive. You probably see two entries for the disk. Click on the one that contains the name of the drive NOT the one that describes the disk (Like for example 320.07 GB Samsung S2 Portable Media).
    Now you see four tabs in the righthand part of the window. They're called "First Aid", "Erase", "RAID" and "Restore". Click on "Erase". This opens the tab with (amongst others) a dropdownbox (Format) and a textbox (Name) . In the dropdownbox select "Mac OS Extended (Journaled). Take note! This selection disables usage of the drive with Windows PC's. If your goal is to share the drive with Windows PC's select the MS-DOS FAT option.
    Enter a label for the drive in the textbox Name. Now click on the button "Erase". This will format the drive.
    Now when it's done you can copy back all the items you've copied to your internal harddrive earlier.
    2) Opening two windows like Explorer on Windows is opening two Finder windows on the Mac. Simply click on the background. In the menubar you items like 'Finder", "File", "Edit", "View", et cetera. Click on "File" and "New Finder Window". Do this two times. Alternatively you can click on the background and press CMD-N two times.
    3) You can right click on the trackpad. This however depends on if you've enabled the feature in System Preferences. In the topmenubar click on the Apple icon and select "System Preferences". Now look for Trackpad in Hardware. Click on it. You see three tabs with the names "Point & Click", "Scroll & Zoom" and "More Gestures". Select "Point & Click" and look for "Secondary Click'. Make sure it is checked and make sure it has written "Click or tap with two fingers" written beneath "Secondary Click". When you're done exit System Preferences. Now select the background and tap with two fingers on the trackpad. This should open the contextuel menu with options like "New Folder", "Get Info" et cetera.

  • Drag and drop to HD

    I want to be able to "Drag and Drop" to the Hard Drive.... but don;t seem to be able to.

    What happens when you enter the admin password?
    Where are you attempting to drag & drop the file from?
    Where are you attempting to drag & drop the file to?
    You are going to have to start answering some of these questions if you wish to get an answer to your questions.
    So far the amount of information you are providing is not enough for any kind of help.
    Allan

  • Drag and drop questions in scjp

    hi all,
    i m planning to write scjp this month..
    can anyone tell me what 'drag n drop' questions like?
    and what kind of questions are generally asked?

    Well you can't change your answer for the drag and drop i mean you can but if you try to go back and try to see your question again for drag and drop then your answer is going to be deleted and then you had to redo the whole thing.
    So i suggest please make your decision at the same time. Drag and drop are not hard i mean something like this
    System.out.printf("Pi is approxmicately", Math.PI);
    Pi is approximately ______
    Place the value for PI 3.14
    3.145
    Something like that Good luck

  • Multiple Selection in JTree, Drag & Drop

    Hi everybody,
    I was wondering if anyone knows how to implement Multiple Selection Drag and Drop in a JTree. I know that there used to be a bug in jdk 1.3 that would allow it, but I suspect 1.4 fixed that. I've implemented Single-selection just fine, but I'm trying do Multiple selection by drawing a selection rectangle and selecting whatever nodes are contained within that rectangle. So far I've implemented it by adding a MouseListener to my JTree, finding which rows are intersected by the rectangle and calling addSelectionRow() to add the TreePaths.
    Is there an easier way to multiple selection now that the bug of 1.3(I hope) has been fixed?

    Okay, now I've implemented the changes in the DragGestureListener, DragSourceListener and DragTargetListener in order to make Multiple Selection possible, however I want to select multiple items the same way Windows Explorer does by:
    1. pressing the left-mouse button & dragging a "selection" rectangle which selects all the nodes contained within that rectangle.
    2. then drag and drop is accomplished by pressing one of the selected nodes and dragging it (along with it's other selected siblings) to the destination.
    In order to accomplish this I added into the Constructor of my JTree extending class this code:
    public class MyTree extends JTree {
    private static int x1, y1, x2, y2;
    public MyTree(DefaultMutableTreeNode rootNode){
    // Other Constructor stuff...
    this.addMouseMotionListener( new MouseMotionListener() {
    public void mouseDragged(MouseEvent e) {  
    x2 = e.getX();
    y2 = e.getY();
    repaint();
    public void mouseMoved(MouseEvent e){     }
    this.addMouseListener(new MouseAdapter(){
    boolean dontRelease = false;
    public void mousePressed( MouseEvent e){
    x1 = e.getX();
    y1 = e.getY();
    int[] rows = getSelectionRows();
    boolean dropSel = true;
    if (rows != null){
    for (int i = 0; i < rows.length; i++){
    Rectangle rect3 = getRowBounds(i);
    if (rect3.contains(x1, y1)){
    dropSel = false;
    dontRelease = true;
    if (dropSel){
    for (int i =0; i < rows.length; i++)
    removeSelectionRow(i);
    public void mouseReleased( MouseEvent e){
    x2 = e.getX();
    y2 = e.getY();
    repaint();
    if (dontRelease == false) {
    Rectangle rect2 = new Rectangle(Math.min(x1, x2),
    Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
    int rowCount = getRowCount();
    for (int i = 1; i < rowCount; i++) {
    Rectangle rect3 = getRowBounds(i);
    if ((rect2.contains(rect3)) || (rect2.intersects(rect3))){
    addSelectionRow(i);
    dontRelease = false;
    repaint();
    The idea was that for every press of the mouse the 1st set of x,y coords. are taken to be the starting corner of the "selection" rectangle, and every point passed over while dragging would be the ending corner of the rectangle (so that in paintComponents, you could actually draw the rectangle).
    Once the mouse is released it will check to see if the selection rectangle had contained or intersected any of the rows (basically I find how many rows are being shown in the tree and go through each one to see if there is intersection/containment). If a row is contained it becomes selected.
    The remaining code in MousePressed is used if node(s) were previously selected it goes through and checks to see if this mouse press is at the location of one of the previous selected nodes - if it is then you can drag that node along with all of its siblings to their destination. However if your mouse press is not on one of the selected nodes - all the nodes should be deselected (like in explorer if you press away from your selection, you lose it). If you select and begin to drag your selection dontRelease should not allow the creation of another "selection" rectangle to occur during the drag-n-drop process.
    -That's how I thought this code should work. Unfortunately as it does most of these things it does not correctly implement rule #2 of Windows Explorer (2. only by dragging one of the selected nodes can you drag them, otherwise you lose them)...currently as long as something is selected you can do a mouse press and drag from another location and as long as that location does not contain a node (like it is a blank area in the JTree), you can drag everything to the destination from when you start dragging.
    I looked around and saw that perhaps it is due to the BasicTreeUI interferring with it's own MouseInputHandler() so I wrote MyTreeUI that extends BasicTreeUI, basically changing only the MouseInputHandler to contain the functions above, and I blocked /**/ those functions from my MyTree Constructor.
    Then in my main class (the one of instantiate my MyTree and my DefaultMutableTreeNodes), I wrote:
    try
    String myTreeUI = "MyTreeUI";
    UIManager.put( "MyTreeUI", myTreeUI );
    UIManager.put( myTreeUI,
    Class.forName( myTreeUI ) );
    } catch( ClassNotFoundException cnfe ) {
    System.err.println( "My Tree UI class not found" );
    System.err.println( cnfe );
    ...thinking that that would be all I needed in order to set MyTreeUI as the JTree's UI, LooknFeel, etc. and although I did not get that ClassNotFoundException when running the program, it did not have any of the functionality that I had written ( I even wrote a System.out.println() in my MousePressed() function in MyTreeUI so that it would let me know that it is working and it never appeared). It was as if the MyTreeUI was not loaded or attached to the JTree. I also called:
    System.out.println("this is UI: " + jTree1.getUIClassID() );
    to see which UI my MyTree instantiate was using and it would printout TreeUI, not MyTreeUI. So I'm wondering what it is I'm doing wrong, I think I am very close to having this thing working but I want to know if anybody here would be able to let me know what I'm doing wrong or what would be the best way to go about implementing this Windows Explorer Multiple Selection JTree.
    Sorry for the long-winded explanation,
    -Meddev24.

  • Did my first back up today using external hard drive and time machine now i can't drag and drop

    Hi, i did my first back up today using time machine it all went onto my external hard drive fine. Now i cant drag and drop anyhting. I assume ive cahnged some settings in time machine but cant figure it out what to do can some one help ?
    thanks

    Brandbasher wrote:
    Thanks Pondini but i have closed time machine, removed external hard drive.
    Did you eject it first?
    Now i cant drag and drop anything on my desk top, or mail or any other application. Ive tried moving folders, documents none of it works. Even moving around documents within a folder, it wont let me chnage the position of files.
    That shouldn't be related to Time Machine (especially if the backups aren't mounted).
    What happens when you try those things?  Do you get any messages?  Anything you can post a screenshot of?
    Have you tried a Restart?
    Does it happen in another user account?  (If you don't have one, create one via System Prefs > Users & Groups.)

  • 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.

  • How do i download video from iphone 4 to a Mac Compatible Seagate GoFlex external hard drive, drag and drop loads an icon without any data?

    I took some video over christmas using my iphone 4 and ever since i have been trying to back up these videos for safe keeping.  When i tried to back up to my computer's c drive only the smallest file would load, the other 2 the icon would appear but no data was present in properties.  so i then tried to drag and drop to my seagate 3tb GoFlex hard drive and i would also get icons but no data.  It seems to me this would be a simple process anyone know the answer? I have windows xp and can seem to move everything else around without any problem just seems to be video as the problem?

    William G 42 wrote:
    , I just can't really do anything with the content going either way.
    Make sure the EHD is Formatted Mac OS Extended (journaled
    Format, Erase, or Reformat a drive
    In iPhoto, Select All the Photos you want to move... Then Goto... > File > Export >
    Choose the settings as seen here
    Click Export and select your External Drive
    Best to create a Folder to put them in... and away you go...
    More Help available from iPhoto Toolbar Help Menu

  • My hard drive crashed on my MacBook Pro crashed and I need to get my CS5 up and running on the new hard drive. I copied what I could onto an external hard drive but when I drag and drop I get directions to uninstall but the uninstall folder I have does sa

    my hard drive crashed on my MacBook Pro crashed and I need to get my CS5 up and running on the new hard drive. I copied what I could onto an external hard drive but when I drag and drop I get directions to uninstall but the uninstall folder I have does says it is an alias that is no longer present. I get error 6. I would like to get my software up and running on the laptop again please

    If you subscribed to iTunes match the songs are kept in icloud. If you didn't but you did save the actual iTunes library folder which should include the music, video, etc.  you should be able to just drag this file back to your machine in the same place and when you open iTunes it will give you a chance to identify the iTunes library folder. 
    You can test this to find out if you saved the media files or not and don't need to attach your nano to do it. 
    You are correct that once you connect the nano it will clear the existing files from it and sync it with the new iTunes library. 
    If you don't have the media files i'm not sure if/how to get them back on a windows ipod.  I have everything backed up regularly because of the horror stories I have seen & heard.

  • How do I transfer files/folders to an external hard drive without "dragging and dropping"?

    Hi, I'm having a lot of trouble trying to transfer folders of pictures and individual pictures to my external hard drive (which God forbid isn't an Apple product). I attempted to drag and drop, which usually works. I have also attempted to find an option in any drop down menu which will allow me to import/transfer/etc. the photos from my Macbook to the external hard drive. This is maddening because the hard drive was immediately compatible and I can see everything that's on it already, I just can't really do anything with the content going either way. Is it because I need to compress things? Something like that? Any help or insight would be greatly appreciated.
    Thanks,
    William

    William G 42 wrote:
    , I just can't really do anything with the content going either way.
    Make sure the EHD is Formatted Mac OS Extended (journaled
    Format, Erase, or Reformat a drive
    In iPhoto, Select All the Photos you want to move... Then Goto... > File > Export >
    Choose the settings as seen here
    Click Export and select your External Drive
    Best to create a Folder to put them in... and away you go...
    More Help available from iPhoto Toolbar Help Menu

Maybe you are looking for