Category

JavaFX drag and drop custom data

JavaFX has greatly simplified Drag and Drop over the mess that was in AWT and then given a zombified life through Swing.

To drag and drop custom data types, i.e. non string data, in Swing, you had to create a DataFlavor class with a MIME type. In JavaFX, all you have to do is create a simple DataFormat object.

And then use it like this in your setOnDragDetected handler.

You may get this problem when putting your object on the Dragboard in OSX:
“…is not a valid UTI string. Cannot set data for an invalid UTI.”

Digging through my XCode documentation on the OSX pasteboard, I found that the format HAS to be the usual reverse domain name class name we’ve been using for years.

The thing is, the JavaFX documentation – and Oracle’s tutorial – makes no mention of this.

I hope this tidbit saves you some time.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.