I want to drag and drop audio file in MediaView. this is my code
@FXML
private void initialize() {
MediaView audioMediaView = new MediaView();
EventHandler<DragEvent> dragOverHandler = event -> {
Object gestSource = event.getGestureSource();
DataFormat mimeAudio = new DataFormat("audio/*");
DataFormat mimeVideo = new DataFormat("video/*");
if ((gestSource != audioMediaView || gestSource != audioListView) &&
(event.getDragboard().hasContent(mimeAudio) ||
event.getDragboard().hasContent(mimeVideo))) {
event.acceptTransferModes(TransferMode.ANY);
System.out.println("Drag Over");
}
event.consume();
};
EventHandler<DragEvent> dragDroppedHandler = event -> {
try {
Dragboard db = event.getDragboard();
boolean success = false;
DataFormat mimeAudio = new DataFormat("audio/*");
DataFormat mimeVideo = new DataFormat("video/*");
if (db.hasContent(mimeAudio) || db.hasContent(mimeVideo)) {
File file = db.getFiles().get(0);
javafx.scene.media.Media media = new javafx.scene.media.Media(file.toURI().toString());
audioMediaView.setMediaPlayer(new MediaPlayer(media));
success = true;
System.out.println("Drag Dropped");
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
} catch (Exception e) {
e.printStackTrace();
}
};
//....other codes
audioMediaView.setOnDragOver(dragOverHandler);
audioMediaView.setOnDragDropped(dragDroppedHandler);
}
But i got this message when i drag an audio file
Java Messsge:DataFormat 'audio/*' already exists.
Any suggestions? Is mime 'audio/*' or 'video/*' incorrect? What can i code drag and drop only audio or video files? Thanks and sorry for my english.
To get rid of error message you just need to put declaration of
outside of
EventHandlers, but it seems to me not the right approach to this task. Instead of dealing withDataFormatyou might simply check the file extension and if it's supported - do your staff with that file:Note, FilenameUtils class is coming from Apache