问题
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