4

I would like to add Drag and Drop support to my JavaFX application. My requirement is that if someone drags files from Native file system and drops to the JavaFX TableView then it must recognize the drop event and how can i get the list of drpped files.

Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
  • It's much easier in SWT, but maybe this one can help you: http://stackoverflow.com/questions/9192371/dragn-drop-files-from-the-os-to-java-application-swing – Romczyk Nov 26 '12 at 11:17

1 Answers1

4

I would use the official JavaFX 2 docs.

http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm

http://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html

So you can understand the methods you'll need to handle the drag events:

target.setOnDragOver

target.setOnDragEntered

target.setOnDragExited

target.setOnDragDropped

Then with the DragEvent on these events you can use the getDragboard() to access the drag content, which inherits Clipboard. From here, you have some methods like getFiles, which is what you need.

Renato
  • 826
  • 9
  • 9
  • 1
    Yes, It's working. Thanks for the reply. Can I do the reverse? I mean drags objects(Rows) from `JavaFX` TableView and drops to Native file system. – Ronak Jain Nov 27 '12 at 06:04
  • The Dragboard should be standard, so in the first link I gave, there is a "source.setOnDragDetected", where you put some content in the Dragboard (you can use putFiles, for instance): /* Put a string on a dragboard */ ClipboardContent content = new ClipboardContent(); content.putString(source.getText()); db.setContent(content); – Renato Dec 03 '12 at 13:55