我正在尝试在TreeView
中实现拖放。
我创建一个单元工厂并捕获相关事件。没关系,但是拖动拖动不会结束操作。
这是测试代码的示例:
@FXML
protected void initialize() {
super.initialize();
treeView.setCellFactory(this::createTreeCell);
}
private TreeCell<Group> createTreeCell(TreeView<Group> tv) {
TreeCell<Group> cell = new TreeCell<>() {
@Override
protected void updateItem(Group group, boolean empty) {
super.updateItem(group, empty);
if(empty || group == null) {
setText("");
}
else {
setText(group.getName());
}
}
};
cell.setOnDragDetected(mouseEvent -> {
System.out.println("Drag detected ... ");
ClipboardContent content = new ClipboardContent();
content.putString("Test");
Dragboard dragboard = cell.startDragAndDrop(TransferMode.COPY);
dragboard.setContent(content);
mouseEvent.consume();
});
cell.setOnDragOver(dragEvent -> {
if(dragEvent.getDragboard().hasString()) {
System.out.println("Drag Over ...");
dragEvent.acceptTransferModes(TransferMode.ANY);
}
dragEvent.consume();
});
cell.setOnDragDropped(dragEvent -> {
System.out.println("Drag Dropped ...");
dragEvent.setDropCompleted(true);
dragEvent.consume();
System.out.println("Is Drop completed: " + dragEvent.isDropCompleted());
});
cell.setOnDragDone(dragEvent -> {
System.out.println("Drag Done ...");
dragEvent.consume();
});
return cell;
}
我收到的跟踪结果是:
Drag detected ...
Drag Over ...
Drag Over ...
.
.
.
Drag Over ...
Drag Over ...
Drag Dropped ...
Is Drop completed: true
Drag Over ...
Drag Over ...
Drag Over ...
Drag Dropped ...
Is Drop completed: true
Drag Over ...
Drag Over ...
Drag Over ...