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

时间:2019-03-27 14:25:14

标签: java javafx drag-and-drop javafx-8

因此,我正在尝试使用javafx库在Java中实现基本的拖放。 setOnDragDetected可以正常工作,但是我将元素拖动到的Pane不能响应任何放置事件。

“要拖动的窗格的问题的实际图像为蓝色,要拖动的元素是矩形。”

我看过不同的教程和文章,它们的源代码也无济于事。 试过有无lambda。

要在窗格上拖动的代码

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

将窗格上使用的代码拖放到:

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

我希望这些甚至侦听器中的print语句都可以正常工作,然后我可以实现其他功能,但是目前看来,侦听器和处理程序甚至无法正常工作

1 个答案:

答案 0 :(得分:1)

我在您的代码中找不到任何明显的错误。因此无法指出确切的问题。也许如果您尝试使用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);
    }
}