0

I implemented a drag and drop of a group to a pane putting an empty string in the clipBoard. , this works perfectly but I need my object visible when dragging.

I have seen in some examples that DataFormat is used and that the object should be serializable , well I'm not sure it could work for my case . I tried using this method but I got an exception.In fact, JavaFX does not support serialization of components using the Java Serializable interface.

I'm using : JavaSE1.7, JavaFX2.2

fabian
  • 80,457
  • 12
  • 86
  • 114
Ines
  • 3
  • 1
  • 4

1 Answers1

1

You can create an image of the Node you are dragging and use that for the dragView:

private EventHandler<? super MouseEvent> onDragDetected() {
    return evt -> {
        Node node = (Node) evt.getSource();
        Dragboard db = node.startDragAndDrop(TransferMode.MOVE);
        db.setDragView(createSnapshot(node), evt.getX(), evt.getY());

        ClipboardContent content = new ClipboardContent();
        content.putString("");
        db.setContent(content);

        evt.consume();
    };
}

private WritableImage createSnapshot(Node node) {
    SnapshotParameters snapshotParams = new SnapshotParameters();
    WritableImage image = node.snapshot(snapshotParams, null);
    return image;
}
jns
  • 6,017
  • 2
  • 23
  • 28