We’ve prepared these code snippets to help you learn JavaFX.

Example 1: Draganddrop

Here are the code snippets:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • DragAndDropExample.java
  • DragAndDropExampleDraft.java
  1. In your editor or IDE, create a file known as DragAndDropExample.java.
  2. Then add the following code:

(a). DragAndDropExample.java

We will need to add a bunch of imports in our class:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

The next step is to extend our class as shown below:

public class DragAndDropExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)
  • void handle(DragEvent event)
  • Circle createCircle(String strokeColor, String fillColor, double x)

The starting point of our Java app will be main method which we create as follows:

    public static void main(String[] args) {

Here is the full code:

package com.jenkov.javafx.draganddrop;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class DragAndDropExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        Circle circle = createCircle("#ff00ff", "#ff88ff",100);

        circle.setOnDragDetected((MouseEvent event) -> {
            System.out.println("Circle 1 drag detected");

            Dragboard db = circle.startDragAndDrop(TransferMode.ANY);

            // set drag and drop data
            ClipboardContent content = new ClipboardContent();
            content.putString("Circle source text");
            db.setContent(content);
        });
        circle.setOnMouseDragged((MouseEvent event) -> {
            //System.out.println("Circle 1 mouse dragged");
            event.setDragDetect(true);
        });

        Circle circle2 = createCircle("#00ffff", "#88ffff",300);

        circle2.setOnDragOver(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                if (event.getGestureSource() != circle2 && event.getDragboard().hasString()) {
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                }

                event.consume();
            }
        });

        circle2.setOnDragDropped((DragEvent event) -> {
            Dragboard db = event.getDragboard();
            if (db.hasString()) {
                System.out.println("Dropped: " + db.getString());
                event.setDropCompleted(true);
            } else {
                event.setDropCompleted(false);
            }
            //event.setDropCompleted(true);
            event.consume();
        });

        Pane pane = new Pane();
        pane.getChildren().add(circle);
        pane.getChildren().add(circle2);

        Scene scene = new Scene(pane, 1024, 800, true);
        primaryStage.setScene(scene);
        primaryStage.setTitle("2D Example");

        primaryStage.show();
    }

    private Circle createCircle(String strokeColor, String fillColor, double x) {
        Circle circle = new Circle();
        circle.setCenterX(x);
        circle.setCenterY(200);
        circle.setRadius(50);
        circle.setStroke(Color.valueOf(strokeColor));
        circle.setStrokeWidth(5);
        circle.setFill(Color.valueOf(fillColor));
        return circle;
    }
}
  1. Next create another file known as DragAndDropExampleDraft.java.
  2. And add the following code:

(b). DragAndDropExampleDraft.java

Our class will do with some imports. Let’s go ahead and add them:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

Extend the class as shown below:

public class DragAndDropExampleDraft extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)
  • void handle(DragEvent event)
  • Circle createCircle(String strokeColor, String fillColor, double x)

The starting point of our Java app will be main method which we create as follows:

    public static void main(String[] args) {

Here is the full code:

package com.jenkov.javafx.draganddrop;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class DragAndDropExampleDraft extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        Circle circle = createCircle("#ff00ff", "#ff88ff",100);

        circle.setOnDragDetected((MouseEvent event) -> {
            System.out.println("Circle 1 drag detected");

            Dragboard db = circle.startDragAndDrop(TransferMode.ANY);

            // set drag and drop data
            ClipboardContent content = new ClipboardContent();
            content.putString("Circle source text");
            db.setContent(content);
        });
        circle.setOnDragDone((DragEvent event) -> {
            System.out.println("Circle 1 Drag and Drop Done");
            event.consume();
        });
        circle.setOnDragDropped((DragEvent event) -> {
            System.out.println("Circle 1 drag dropped");
        });

        circle.setOnMouseDragged((MouseEvent event) -> {
            //System.out.println("Circle 1 mouse dragged");
            event.setDragDetect(true);
        });
        circle.setOnMousePressed((MouseEvent event) -> {
            //circle.setMouseTransparent(true);
        });
        circle.setOnMouseReleased((MouseEvent event) -> {
            //circle.setMouseTransparent(false);
        });

        Circle circle2 = createCircle("#00ffff", "#88ffff",300);

        circle2.setOnMouseDragEntered((MouseEvent event) -> {
            circle2.setStroke(Color.valueOf("#ff8888"));
        });
        circle2.setOnMouseDragExited((MouseEvent event) -> {
            circle2.setStroke(Color.valueOf("#00ffff"));
        });

        circle2.setOnMouseDragReleased((MouseEvent event) -> {
            //System.out.println("Circle 2 -> Drag and dropped...");
        });

        circle2.setOnDragOver(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                if (event.getGestureSource() != circle2 && event.getDragboard().hasString()) {
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                }

                event.consume();
            }
        });

        circle2.setOnDragDropped((DragEvent event) -> {
            Dragboard db = event.getDragboard();
            if (db.hasString()) {
                System.out.println("Dropped: " + db.getString());
                event.setDropCompleted(true);
            } else {
                event.setDropCompleted(false);
            }
            //event.setDropCompleted(true);
            event.consume();
        });

        Pane pane = new Pane();
        pane.getChildren().add(circle);
        pane.getChildren().add(circle2);

        Scene scene = new Scene(pane, 1024, 800, true);
        primaryStage.setScene(scene);
        primaryStage.setTitle("2D Example");

        primaryStage.show();
    }

    private Circle createCircle(String strokeColor, String fillColor, double x) {
        Circle circle = new Circle();
        circle.setCenterX(x);
        circle.setCenterY(200);
        circle.setRadius(50);
        circle.setStroke(Color.valueOf(strokeColor));
        circle.setStrokeWidth(5);
        circle.setFill(Color.valueOf(fillColor));
        return circle;
    }
}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License