问题
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
回答1:
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
来源:https://stackoverflow.com/questions/37237837/javafx-get-drag-drop-target-folder-after-drop-has-been-done