拖放方法不会触发

时间:2019-05-29 14:21:08

标签: java javafx drag-and-drop fxml scenebuilder

我有一个按钮和一个堆栈窗格。现在,我希望能够将该按钮拖动到该堆栈窗格上:

enter image description here

出于测试目的,我将所有可能的拖动事件添加到两个组件上:

enter image description here

FXML:

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.TestController">
   <children>
      <Button fx:id="source" layoutX="119.0" layoutY="172.0" mnemonicParsing="false" onDragDetected="#dragDetected" onDragDone="#dragDone" onDragDropped="#dragDropped" onDragEntered="#dragEntered" onDragExited="#dragExited" onDragOver="#dragOver" onMouseDragEntered="#mouseDragEntered" onMouseDragExited="#mouseDragExited" onMouseDragOver="#mouseDragOver" onMouseDragReleased="#mouseDragReleased" text="Drag Me!" />
      <StackPane fx:id="stackPane" layoutX="237.0" layoutY="110.0" onDragDetected="#dragDetected" onDragDone="#dragDone" onDragDropped="#dragDropped" onDragEntered="#dragEntered" onDragExited="#dragExited" onDragOver="#dragOver" onMouseDragEntered="#mouseDragEntered" onMouseDragExited="#mouseDragExited" onMouseDragOver="#mouseDragOver" onMouseDragReleased="#mouseDragReleased" prefHeight="150.0" prefWidth="200.0" style="-fx-border-color: black; -fx-background-color: lightgray;" />
   </children>
</AnchorPane>

控制器:

package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;

public class TestController implements Initializable {

    @FXML
    private Button source;
    @FXML
    private StackPane stackPane;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    @FXML
    private void mouseDragEntered(MouseDragEvent event) {
        System.out.println("mouseDragEntered");
    }

    @FXML
    private void mouseDragExited(MouseDragEvent event) {
        System.out.println("mouseDragExited");
    }

    @FXML
    private void mouseDragReleased(MouseDragEvent event) {
        System.out.println("mouseDragReleased");
    }

    @FXML
    private void mouseDragOver(MouseDragEvent event) {
        System.out.println("mouseDragOver");
    }

    @FXML
    private void dragDetected(MouseEvent event) {
        System.out.println("dragDetected");
    }

    @FXML
    private void dragEntered(DragEvent event) {
        System.out.println("dragEntered");
    }

    @FXML
    private void dragDone(DragEvent event) {
        System.out.println("dragDone");
    }

    @FXML
    private void dragExited(DragEvent event) {
        System.out.println("dragExited");
    }

    @FXML
    private void dragOver(DragEvent event) {
        System.out.println("dragOver");
    }

    @FXML
    private void dragDropped(DragEvent event) {
        System.out.println("dragDropped");
    }

}

当我拖放按钮时,仅触发dragDetected功能。以下所有步骤(即dragOverdragDone等)都不会被识别。

1 个答案:

答案 0 :(得分:4)

添加

Dragboard db = source.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();

dragDetected