JavaFX在拖放完成后获取拖放目标文件夹

标签 java javafx drag-and-drop

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

The Problem:

我已经为 Button 设置了下面的代码,它的全名代表文件路径。所以当 Button 被拖到文件夹或桌面时(假设在Windows😛) 它被用户的操作系统复制。

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

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

    }

编辑:(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());
            });

我得到这个输出,你可以看到 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

最佳答案

我做了一些研究,可能不可能。例如这个线程:Get file path for drag-n-drop file on Windows Explorer

你可以试试这个解决方法:

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

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

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

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

关于JavaFX在拖放完成后获取拖放目标文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37237837/

相关文章:

java - Java 的 LinkedList 是否优化为在必要时反向执行 get(index)?

java - Java Mockito 模拟方法调用线程安全吗?

java - 用于 Facebook 登录的 jsoup

java - 在 run 方法中使用同步块(synchronized block)的效果很奇怪

java - 按钮事件处理程序更新文本字段问题

Swift:在 NSOutlineView 中拖放

java - 最简单的通过鼠标旋转相机不起作用

css - 在 JavaFX 应用程序中制作 Windows Phone 8 等量大小的磁贴

ios - 在 UITextView 中拖放时显示文本光标

css - 动态获取内部 Div 容器 ID