Rilakkuma February 2016

TreeView using Drag and Drop incorrect getGestureSource()

I have a simple tree view with drag and drop enabled. But, on setOnDragOver() call, I receive event with gesture source. The source got is not the cell that starts the dragging (via setOnDragDetected() call), but the pane that contains the tree view. What am I missing ?

My code is shown below:

        tree.setCellFactory(
        new Callback<TreeView<String>, TreeCell<String>>() {

            @Override
            public TreeCell<String> call(
                    final TreeView<String> param) {

                TreeCell<String> treeCell =
                    new TreeCell<String>() {

                    @Override
                    protected void updateItem(
                            final String value,
                            final boolean empty) {

                        super.updateItem(value, empty);

                        if (!empty && (value != null)) {

                            setText(value);
                            setGraphic(getTreeItem().getGraphic());

                        } else {

                            setText(null);
                            setGraphic(null);
                        }
                    }
                };

                treeCell.setOnDragDetected(event -> {

                    Dragboard dragBoard =
                        startDragAndDrop(TransferMode.MOVE);

                    ClipboardContent content = new ClipboardContent();

                    content.putString(treeCell.getTreeItem().getValue());

                    dragBoard.setContent(content);

                    event.consume();
                });

                treeCell.setOnDragOver(event -> {

                    // Here is I've got pane instead of cell
                    Object source = event.getGestureSource();

                    if ((event.getGestureSource() != treeCell)
                        && event.getDrag        

Answers


James_D February 2016

The gesture source is the node on which startDragAndDrop(...) was called; you are calling it on whatever node instance you are currently in (I guess you have some custom subclass of Pane, or something).

So you need

treeCell.setOnDragDetected(event -> {

    Dragboard dragBoard =
        treeCell.startDragAndDrop(TransferMode.MOVE);

    // ...
});

Post Status

Asked in February 2016
Viewed 2,037 times
Voted 4
Answered 1 times

Search




Leave an answer