Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
256 views
Welcome To Ask or Share your Answers For Others

1 Answer

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("
")));
        evt.setDropCompleted(true);
    });

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...