TheHoop
TheHoop

Reputation: 375

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.

Upvotes: 33

Views: 20711

Answers (3)

Renis1235
Renis1235

Reputation: 4710

I solved this by adding two event handlers. One for DragDropped event and the other for DragOver event.

e.g:

@FXML
void handleFileOverEvent(DragEvent event)
{
    Dragboard db = event.getDragboard();
    if (db.hasFiles())
    {
        event.acceptTransferModes(TransferMode.COPY);
    }
    else
    {
        event.consume();
    }
}

@FXML
void handleFileDroppedEvent(DragEvent event)
{
    Dragboard db = event.getDragboard();
    File file = db.getFiles().get(0);

    handleSelectedFile(file);
}

Else it did not work for me, dragging the file over my pane, didn't trigger anything.

Upvotes: 2

Moritz Schmidt
Moritz Schmidt

Reputation: 2823

When working with Drag and Drop events, you could try the following:

Obtain a Dragboard-object of the DragEvent and work with the method getFiles:

private void handleDragDropped(DragEvent event){
    Dragboard db = event.getDragboard();
    File file = db.getFiles().get(0);
}

Upvotes: 3

WillShackleford
WillShackleford

Reputation: 7018

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

}

Upvotes: 40

Related Questions