TreeView拖放未完成

时间:2018-12-21 10:28:40

标签: javafx drag-and-drop treeview

我正在尝试在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 ...

0 个答案:

没有答案