JavaFx 8 TreeCell Drag and Drop

1k views Asked by At

The objective is to use drag and drop on a treeView to move the nodes on itself.
I have seen several examples where the events are handled in a redefined TreeCell, but only the detected event is triggered.
This is my code:

public class TreeCellImpl extends TreeCell<TreeItemContent> {

    public TreeCellImpl() {
        setOnDragEntered(e -> {
            System.out.println(" Entered ");
            e.consume();
        });
        setOnDragDetected(e -> {
            System.out.println(" Detected ");
            startDragAndDrop(TransferMode.MOVE);
            e.consume();
        });
        setOnDragDone(e -> {
            System.out.println(" Done ");
            e.consume();
        });
        setOnDragDropped(e -> {
            System.out.println(" Dropped ");
            e.setDropCompleted(true);
            e.consume();
        });
        setOnDragExited(e -> {
            System.out.println(" Exited ");
            e.consume();
        });
        setOnDragOver(e -> {
            System.out.println(" Over ");
            e.consume();
        });
     }
}

Where i wrong ?

1

There are 1 answers

0
Uluk Biy On BEST ANSWER

It seems the event handlers are not triggered until you put some content to Dragboard:

    setOnDragDetected(e -> {
        System.out.println(" Detected ");
        Dragboard db = startDragAndDrop(TransferMode.MOVE);
        ClipboardContent content = new ClipboardContent();
        content.putString( "Hello!" );
        db.setContent(content);
        e.consume();
    });

You may also choose to use the other type of drag-n-drop mechanism described in MouseEvent. I.e. MouseDragEvent.