将多个文件拖放到javaFX中 [英] Drag and drop multiple files into javaFX

查看:1118
本文介绍了将多个文件拖放到javaFX中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手。我正在构建一个小型应用程序,以帮助我正常工作,基本上处理几个文件文本文件,并添加这些文件包含的文本符号的数量。我想了解如何将多个文件放入javaFX场景,因为 handle(DragEvent事件)只接受一个文件。

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.

推荐答案

您可以在 DragEvent 中明确接受多个文件。 >
以下示例显示了放到场景中的文件名:

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

这篇关于将多个文件拖放到javaFX中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆