0

I'm trying to implement Drag-and-Drop in TreeView. I create a cell factory and capture the relevant events. It's all right, but drag drag does not end the operation.

This is a sample of the test code:

@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;
}

The outcome of the trace I receive is:

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 ...
mr mcwolf
  • 2,574
  • 2
  • 14
  • 27
  • I have not the same behaviour. `Drag Done` is well displayed after the `Is Drop completed : true`. – Pagbo Dec 21 '18 at 10:57
  • the problem may be in my operating system. I use Linux – mr mcwolf Dec 21 '18 at 11:03
  • Maybe, And I have neither linux nor available VM for the moment. – Pagbo Dec 21 '18 at 13:15
  • 1
    If you're using Linux and JavaFX 11 maybe [this answer](https://stackoverflow.com/a/53351433/6395627) can help. – Slaw Dec 21 '18 at 17:38
  • @Slaw, I apologize for the late reaction. The problem is in the GTK version. After return to GTK2 (`-Djdk.gtk.version=2`) everything work correctly. 10x for support. – mr mcwolf Dec 27 '18 at 06:46

0 Answers0