删除完成后,JavaFX将拖放目标文件夹

GOX*_*LUS 20 java drag-and-drop javafx

有关Java Oracle社区的问题(https://community.oracle.com/thread/3934986)

问题:

我已经设置了下面的代码Button,它的全名表示文件路径.所以当它Button被拖动到文件夹或桌面时(让我们说Windows),它会被用户的操作系统复制.

我想获取已完成放置的目标文件夹.

码:

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();

    }
Run Code Online (Sandbox Code Playgroud)

编辑:(29/09/2016)

下降完成后:

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());
            });
Run Code Online (Sandbox Code Playgroud)

我得到这个输出,你可以看到 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
Run Code Online (Sandbox Code Playgroud)

Pio*_*zke 4

我做了一些研究,这可能是不可能的。例如此线程:在 Windows 资源管理器上获取拖放文件的文件路径

您可以尝试这个解决方法:

  • 在 setOnDragDetected() 中,复制要删除的文件并将此复制的文件路径放入 content.putFile
  • 将 TransferMode.COPY 更改为 TransferMode.MOVE
  • 开始监视复制的文件以检测它被移动到的位置 - 现在这是棘手的部分。其中一些库可能会有所帮助:Java:文件重命名检测

这只是一个解决方法的想法(我没有完全测试它,但值得检查)

编辑1:值得一读。如何在 Windows 上进行文件夹检测:

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