Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx Drag and Drop a file INTO a program

Hey there community I was wondering if is possible to create a program that allows for the user to Drag a file from anywhere on there hard drive (the desktop, documents folder, videos folder) and drop it into the window of the program.

I am creating a media player and I want to be able to play a video by dragging and dropping a MP4 into the window. Do I need to store the file in a variable, or just the location of the file into a variable. Also, it is important I keep support for cross platform.

I am using JavaFx with java 7 update 79 jdk.

Thanks in advance.

like image 378
TheHoop Avatar asked Sep 12 '15 01:09

TheHoop


People also ask

How do you drag and drop in Javafx?

A drag-and-drop gesture happens as follows: The user click a mouse button on a gesture source, drags the mouse, and releases the mouse button on a gesture target.


1 Answers

Here is a simple drag and drop example that just sets the file name and location. Drag files to it and it shows their name and location. Once you know that it should be a completely separate matter to actually play the file. It is primarily taken from Oracle's documentation: https://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm

A minimal implementation needs two EventHandler s set OnDragOver and OnDragDropped.

public class DragAndDropTest extends Application {      @Override     public void start(Stage primaryStage) {         Label label = new Label("Drag a file to me.");         Label dropped = new Label("");         VBox dragTarget = new VBox();         dragTarget.getChildren().addAll(label,dropped);         dragTarget.setOnDragOver(new EventHandler<DragEvent>() {              @Override             public void handle(DragEvent event) {                 if (event.getGestureSource() != dragTarget                         && event.getDragboard().hasFiles()) {                     /* allow for both copying and moving, whatever user chooses */                     event.acceptTransferModes(TransferMode.COPY_OR_MOVE);                 }                 event.consume();             }         });          dragTarget.setOnDragDropped(new EventHandler<DragEvent>() {              @Override             public void handle(DragEvent event) {                 Dragboard db = event.getDragboard();                 boolean success = false;                 if (db.hasFiles()) {                     dropped.setText(db.getFiles().toString());                     success = true;                 }                 /* let the source know whether the string was successfully                   * transferred and used */                 event.setDropCompleted(success);                  event.consume();             }         });           StackPane root = new StackPane();         root.getChildren().add(dragTarget);          Scene scene = new Scene(root, 500, 250);          primaryStage.setTitle("Drag Test");         primaryStage.setScene(scene);         primaryStage.show();     }      /**      * @param args the command line arguments      */     public static void main(String[] args) {         launch(args);     }  } 
like image 180
WillShackleford Avatar answered Sep 19 '22 14:09

WillShackleford