20

Question on Java Oracle Community (https://community.oracle.com/thread/3934986)

The Problem:

I have set the code below for a Button which it's full name represents a file path.So when the Button is dragged to a folder or desktop(let's say in Windows) it is copied by user's Operating System.

I want to get the target folder on which the drop has been done.

Code:

button.setOnDragDetected(event -> {

        /* allow copy transfer mode */
        Dragboard db =  startDragAndDrop(TransferMode.COPY,TransferMode.LINK);

        /* put a string on dragboard */
        ClipboardContent content = new ClipboardContent();

        /*Adding a file into the ClipboardContent*/
        content.putFiles(Arrays.asList(new File(getSongPath())));

        ........//not showing this code....

        db.setContent(content);

        event.consume();

    }

Edit:(29/09/2016)

When the drop is done:

setOnDragDone(d -> {
                System.out.println(
                        "Drag Done,is drop completed?" + d.isDropCompleted() + " , is accepted?" + d.isAccepted());
                System.out.println("Accepted Mode:" + d.getAcceptedTransferMode());
                System.out.println(" Target:" + d.getTarget() + " Gestoure Target:" + d.getGestureTarget());
            });

I get this output where you can see that the Gesture Target == null

Drag Done:true , s drop completed?false , is accepted?true
Accepted Mode:COPY
Target:SmartController$TableViewer@112437[styleClass=table-view] Gesture Target:null
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 1
    Hava a look at this question: http://stackoverflow.com/questions/25488319/how-to-update-progress-of-the-progress-bar-to-show-the-progress-of-copying-a-fil – jns May 15 '16 at 14:03
  • I stuck at the same problem... Did you find any solution? – Harald Aug 28 '16 at 10:22
  • 1
    @user364044 I have also posted this question on Oracle Java Community and no answers. :( – GOXR3PLUS Aug 28 '16 at 14:04
  • @jewelsea I have made this question also in Oracle Java Community but either no one knows how to answer or JavaFX has no such functionality,so i posted this comment hoping that you may know the answer or a work around :) .I need that functionality. – GOXR3PLUS Sep 24 '16 at 00:20
  • 1
    (Not sure if this is obvious or not) setOnDragDetected is fired when the drag starts (the drag target is unknown). Check setOnDragDone and dsetOnDragExited. (still cannot guarantee that the path will be there) – Piotr Reszke Sep 27 '16 at 16:02
  • @Piotr R I edited the question.No luck.Seems that a library needs to be created for that... – GOXR3PLUS Sep 29 '16 at 04:25
  • Maybe if you could describe why do you need this folder, then finding the workaround would be easier. (If it's some post-processing it could be done before the drop - just guessing) – Piotr Reszke Sep 30 '16 at 07:00
  • @Piotr R i want it cause when the drop is done,to create a `Thread` that checks the progress of copy or move procedure ,so i can show a notification after it has been completed along with a progress bar. – GOXR3PLUS Sep 30 '16 at 07:14

1 Answers1

4

I did some research and it is probably not possible. For example this thread: Get file path for drag-n-drop file on Windows Explorer

You may try this workaround:

  • in the setOnDragDetected() make a copy of the file you would like to drop and put this copied file path in the content.putFile
  • change TransferMode.COPY to TransferMode.MOVE
  • start monitoring the copied file to detect where it was moved - now this is the tricky part. Some of these libraries may help: Java: File Renaming Detection

This is just an idea for workaround (I didn't fully test it but worth checking)

EDIT 1: Worth reading. How folder detection can be done on Windows:

http://www.codeproject.com/Articles/23207/Drag-and-Drop-to-Windows-Folder-C

Community
  • 1
  • 1
Piotr Reszke
  • 1,576
  • 9
  • 21