Drag and drop from javaFX scene to windows explorer

跟風遠走 提交于 2019-12-07 18:33:55

问题


Is there any way to Drag and drop from javaFX scene to windows explorer?


回答1:


Yes there is You should use the onDragDetected function to start your dragEvent and the onDragDone function for doing whatever you want after finishing the drag & drop.

Here an example:

final Text source = new Text(50, 100, "DRAG ME");

    source.setOnDragDetected(new EventHandler <MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start drag-and-drop gesture*/
            System.out.println("onDragDetected");

            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);

            /* put a string on dragboard */
            ClipboardContent content = new ClipboardContent();
            content.putString(source.getText());
            db.setContent(content);

            event.consume();
        }


source.setOnDragDone(new EventHandler <DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture ended */
            System.out.println("onDragDone");
            /* if the data was successfully moved, clear it */
            if (event.getTransferMode() == TransferMode.MOVE) {
                source.setText("");
            }

            event.consume();
        }
    });


来源:https://stackoverflow.com/questions/23773431/drag-and-drop-from-javafx-scene-to-windows-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!