JavaFx Drag and Drop a file INTO a program
Asked Answered
E

3

33

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.

Emogeneemollient answered 12/9, 2015 at 1:3 Comment(4)
I recommend Java 8+ for JavaFX development. Also, remove the java-8 tag if the question is not related to Java 8.Minne
@Minne Yeah i mean I don't think the syntax changes all that much for the drag and drop function, so the version is most likely irrelevant. Anyhow I am asking about the code not what version to use. Ty for your response though!Emogeneemollient
@Emogeneemollient I think there were more changes to syntax between versions Java 7 and Java 8 than there were in any of the previous 18 year history of Java (especially applied to things like event handlers). Please make the tags and the question consistent. It would also be helpful if you would post some code you have tried.Cladoceran
@Cladoceran I am currently using Java 8 and have referred to this question for help. I don't see any significant differences in syntax between the two versions, especially anything I can't immediately fix and I can easily say that this question has helped me solve my problem. With the small amount of relevant changes that have occurred between these two versions of Java, I'd say that it's ok to keep the Java-8 tag, as it may help others using Java 8 in the future.Osullivan
R
40

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

}
Ricer answered 16/9, 2015 at 0:29 Comment(1)
Please use filters too so that user can only choose mp4 or video extensions onlySycophancy
C
3

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);
}
Corollary answered 18/6, 2018 at 19:10 Comment(0)
T
2

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.

Tailrace answered 25/11, 2021 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.