У меня есть кнопка и панель стека. Теперь я хочу перетащить эту кнопку на эту область стека:
В целях тестирования я добавил все возможные события перетаскивания на оба компонента:
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
. Все последующие шаги (т.е. dragOver
, dragDone
и т. Д.) Не будут распознаны.