JavaFX Drag and Drop Images

If you deal with files in your JavaFX app, then you need to have drag and drop integration. You’ll be amazed at how easy drag-and-drop is in JavaFX.

The following JavaFX image drag-and-drop example only takes three files. You can just copy them into your JavaFX project, and run them if you’d like.

The resulting application allows you to drag an image file onto your window. As the image file moves over the window, it will change in a OS specific way, indicating that you can drop the file onto the open window. Once you drop the image file on to the window, the image will be displayed in the window.

The three files that you will need for this project are the file, the image-view.fxml file, and the file.

Here is the source for your Main class.

package com.whatisjavafx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {
	public void start(Stage stage) {
	  try {
      URL url = 
      BorderPane root = FXMLLoader.load(url);

      Scene scene = new Scene(root);
      .setTitle("JavaFX Drop Image");;
    } catch(Exception e) {
	public static void main(String[] args) {


All the Main class does is grab the FXML file to use as a root. If you’ve been through the¬†tutorial entitled, What is JavaFX, you are already familiar with the parts of the Main class generally found in JavaFX.

Here is the source for your FXML file.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>

      preserveRatio="true" />


You can create this FXML file using SceneBuilder. All it is, is a BorderPane containing an ImageView. The ImageView implements onDragDropped and onDragOver. You’ll see more about them in the Controller.

Here is the source for your Controller.

package com.whatisjavafx;

import java.util.List;

import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;

public class ImageDropController {

    private ImageView myImageView;

    void imageDragOver(DragEvent de) {
      Dragboard board = de.getDragboard();
      if (board.hasFiles()) {

    void imageDropped(DragEvent de) {
      try {
        Dragboard board = de.getDragboard();
        List<File> phil = board.getFiles();
        FileInputStream fis;
          fis = new FileInputStream(phil.get(0));
        Image image = new Image(fis);
      } catch (FileNotFoundException e) {

    void initialize() {
      // you could initialize 
      // JavaFX Controls here


The real meat of this example is found in imageDragOver() and in imageDropped(). The method imageDragOver() checks to make sure a file is being dragged onto the ImageView, then says, “Yeah, I’ll take it.”

The imageDropped() method looks for the first of any files dragged onto the ImageView, then converts it to a Image object. That Image object is then used to set the ImageView‘s image. Pretty simple really.

Now compile, run, ¬†and start dragging. JavaFX’s fun.

Leave a Reply

Your email address will not be published. Required fields are marked *