Drag and drop multiple files into javaFX

☆樱花仙子☆ 提交于 2019-12-13 11:12:28

问题


I'm pretty new to Java. I'm building a samll app to help in my normal work, basically to process several files text files and add up the number of text symbols contained by those files. I would like to understand how to drop multiple files into a javaFX scene, since handle(DragEvent event) accepts only one file.


回答1:


You can clearly accept multiple files in a DragEvent.
The following example displays the file names dropped to the scene:

@Override
public void start(Stage primaryStage) {
    Text text = new Text();
    StackPane root = new StackPane(text);

    root.setOnDragOver(evt -> {
        if (evt.getDragboard().hasFiles()) {
            evt.acceptTransferModes(TransferMode.LINK);
        }
    });
    root.setOnDragDropped(evt -> {
        text.setText(evt.getDragboard().getFiles().stream().map(File::getAbsolutePath).collect(Collectors.joining("\n")));
        evt.setDropCompleted(true);
    });

    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}


来源:https://stackoverflow.com/questions/49920490/drag-and-drop-multiple-files-into-javafx

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