0

I have a rectangle, and another type of node on the right which is a VBox whose children are a HBox containing the dashed rectangles above the line, a separator and the dashed rectangle below the line. I am able to drag and drop the left rectangle, but I want to be able to drag it over one of the smaller dashed rectangles, and have the dragged rectangle take the dashed one's place. How can I go about doing this? Example code of what I have so far:

package sample;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Example extends Application {
    @Override
    public void start(Stage stage) {

        Rectangle r1 = new Rectangle();
        r1.setX(50);
        r1.setY(50);
        r1.setWidth(200);
        r1.setHeight(100);
        r1.setFill(Color.rgb(30,30,30));
        r1.setStroke(Color.WHITE);

        Rectangle r2 = new Rectangle();
        r2.setX(300);
        r2.setY(50);
        r2.setWidth(100);
        r2.setHeight(50);
        r2.setFill(Color.rgb(30,30,30));
        r2.setStroke(Color.WHITE);
        r2.setStyle("-fx-stroke-dash-array: 2");

        makeDraggable(r1);
        makeDraggable(r2);

        Group root = new Group(r2, r1);
        Scene scene = new Scene(root, 800, 400);
        scene.setFill(Color.rgb(30, 30, 30));
        stage.setTitle("Example");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String args[]) {
        launch(args);
    }


    private void makeDraggable(Node n) {
        final Delta dragDelta = new Delta();
        n.setOnMousePressed(mouseEvent -> {
            dragDelta.x = n.getLayoutX() - mouseEvent.getSceneX();
            dragDelta.y = n.getLayoutY() - mouseEvent.getSceneY();

        });
        n.setOnMouseDragged(mouseEvent -> {
            n.setLayoutX(mouseEvent.getSceneX() + dragDelta.x);
            n.setLayoutY(mouseEvent.getSceneY() + dragDelta.y);
        });
    }

}

class Delta { double x, y; }

Left rectangle is the source, small rectangles with dashed lines on the right are targets

Joe Tebbett
  • 3
  • 1
  • 2
  • Any node that you want to drop on needs target events similar to the given link. https://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm – SedJ601 May 06 '20 at 15:11
  • [mcve] please .. – kleopatra May 06 '20 at 15:26
  • @kleopatra just added one, hope that's useful – Joe Tebbett May 06 '20 at 15:53
  • @Sedrick I've looked at that documentation and similar pages, the issue I have is that I'm trying to drag and drop a node, not just drag and drop some text/an image/some other data from a node via a dragboard – Joe Tebbett May 06 '20 at 15:55
  • Don't try to drag and drop a node. Simulate dragging the node. When you drop the node, create a new node that has all the same attributes as the old node. Remove the old node. – SedJ601 May 06 '20 at 16:04
  • I took a quick look at this. See if it helps. https://monograff76.wordpress.com/2015/03/01/developing-a-drag-and-drop-ui-in-javafx-part-5-draggable-nodes/ – SedJ601 May 06 '20 at 16:09
  • @Sedrick why not drag and drop the node? – Joe Tebbett May 06 '20 at 16:21
  • https://stackoverflow.com/questions/55709178/how-to-drag-and-drop-button-onto-gridpane/55709991#55709991 – SedJ601 May 06 '20 at 17:18
  • For one, `Nodes` can't just be dragged around without setting their X and Y values. Two, Node can't exist in two different parent nodes at the same time. – SedJ601 May 06 '20 at 17:20
  • `Dragboard->setDragView` looks promising. https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/input/Dragboard.html – SedJ601 May 06 '20 at 17:27

0 Answers0