我尝试在网格窗格中执行拖放事件,但没有成功。检测到拖动,但未检测到拖动或放置事件。我做错了什么?
这是我尝试过的代码:
public class FXMLDocumentController implements Initializable {
@FXML
private GridPane grid;
@Override
public void initialize(URL url, ResourceBundle rb) {
StackPane sourcePane = new StackPane(new Text("Source"));
StackPane targetPane = new StackPane(new Text("Target"));
sourcePane.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
sourcePane.startDragAndDrop(TransferMode.ANY);
System.out.println("Drag detected");
event.consume();
}
});
targetPane.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
System.out.println("Drag over detected");
event.consume();
}
});
targetPane.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
System.out.println("Drop detected");
event.consume();
}
});
grid.add(sourcePane, 1, 1);
grid.add(targetPane, 0, 1);
}
}