拖放侦听器和处理程序不起作用 [JavaFX]

Drag and drop listeners and handlers not working [JavaFX]

所以我正在尝试使用 javafx 库在 java 中实现基本的拖放操作。 setOnDragDetected 工作正常,但我将元素拖入的窗格没有响应任何放置事件。

"Actual image of the problem the pane to be dragged into is in blue, the elements to be dragged are the rectangles."

我查看了不同的教程和文章,它们的源代码也没有帮助。 尝试使用和不使用 lambda。

要拖动的Pane上使用的代码

public abstract class VueEtapeIG extends Pane {
       public VueEtapeIG(...){
            //some code..
            this.setOnDragDetected((MouseEvent event) -> {
            //activate();
            Dragboard db = this.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            // Store node ID in order to know what is dragged.
            content.putString(this.getId());
            db.setContent(content);
            System.out.println("setOnDragDetected");
            event.consume();
        });
    }
}

要拖入的Pane上使用的代码:

public class VueDessin extends Pane implements Observer
{
     public VueDessin(...){
        //some code..
        setOnDragOver((DragEvent event) -> {
            if (event.getGestureSource() != this && 
                      event.getDragboard().hasString()) {
                System.out.println("acceptTransferModes");
                event.acceptTransferModes(TransferMode.MOVE);
            }
            System.out.println("setOnDragOver");
            event.consume();
        });

        setOnDragDropped((DragEvent event) -> {
            Dragboard db = event.getDragboard();
            System.out.println("Dropped!");
            // Get item id here, which was stored when the drag started.
            boolean success = false;
            // If this is a meaningful drop...
            if (db.hasString()) {
                String nodeId = db.getString();
                //Search for the etape dropped
            }
            event.setDropCompleted(success);
            event.consume();
        });
    }
}

我希望这些 even listener 中的 print 语句能够工作,然后我可以进一步实现其他功能,但目前看来 listeners 和 handlers 甚至都没有工作

我在您的代码中找不到任何明显的错误。所以无法指出确切的问题。可能如果你尝试使用 Minimal, Complete, and Verifiable example 你可能会知道。请检查下面我尝试过的演示,它运行良好。尝试找出您的代码出了什么问题(与此相比)。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class DragDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        Scene sc = new Scene(root, 600, 600);
        stage.setScene(sc);
        stage.show();

        HBox hb = new HBox();
        VBox imageBox = new VBox();
        Node node1 = buildNode("red");
        Node node2 = buildNode("yellow");
        imageBox.getChildren().addAll(node1,node2);

        StackPane displayBox = new StackPane();
        displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;");
        HBox.setHgrow(displayBox,Priority.ALWAYS);
        hb.getChildren().addAll(imageBox,displayBox);
        root.getChildren().add(hb);

        displayBox.setOnDragOver(event -> {
            if (event.getGestureSource() != displayBox &&
                    event.getDragboard().hasString()) {
                event.acceptTransferModes(TransferMode.MOVE);
            }
            event.consume();
        });

        displayBox.setOnDragEntered(event -> {
            if (event.getGestureSource() != displayBox && event.getDragboard().hasString()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;-fx-opacity:.4;-fx-background-color:"+event.getDragboard().getString());
            }
            event.consume();
        });

        displayBox.setOnDragExited(event -> {
            if(!event.isAccepted()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;");
                event.consume();
            }
        });

        displayBox.setOnDragDropped(event -> {
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;-fx-background-color: "+db.getString());
                success = true;
            }
            event.setDropCompleted(success);
            event.consume();
        });

    }
    private Node buildNode(String color){
        StackPane node = new StackPane();
        node.setPrefSize(200,200);
        node.setStyle("-fx-background-color:"+color);
        node.setOnDragDetected(event -> {
            Dragboard db = node.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putImage(node.snapshot(new SnapshotParameters(),null));
            content.putString(color);
            db.setContent(content);
            event.consume();
        });
        return node;
    }

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