Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX get drag && drop target folder after drop has been done

Tags:

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
like image 407
GOXR3PLUS Avatar asked May 15 '16 11:05

GOXR3PLUS


People also ask

How to drag JavaFX?

Moreover, drag-and-drop can be implemented between a JavaFX application and a third-party (native) application such as Windows Explorer or a desktop. A drag-and-drop gesture happens as follows: The user click a mouse button on a gesture source, drags the mouse, and releases the mouse button on a gesture target.

What does drag-and-drop operation perform?

A drag-and-drop operation is a data transfer between two objects: a gesture source and a gesture target. The gesture source and gesture target can be the following objects: Nodes. Scenes.


1 Answers

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

like image 51
Piotr Reszke Avatar answered Oct 10 '22 04:10

Piotr Reszke