title: JavaFX DragAndDrop with custom DataFormat
body: <h2>The Overview</h2>
  
  <p>I am setting up drag &amp; drop in a Java FX application. When I try to grab custom data from the <code>Dragboard</code> I am getting a <code>java.nio.HeapByteBuffer</code> instead of an <code>Object</code> as the JavaDoc stuggest. This byte buffer cannot be <em>cast</em> to my original data type.</p>
  
  <p>I'm working in Scala so the syntax it a tiny bit different but you get the idea. However, perhaps it's because I'm in Scala that I'm getting this <code>HeapByteBuffer</code> instead of a regular <code>Object</code>?</p>
  
  <h2>The Details</h2>
  
  <p>Ok, here I'm going to create JavaFX control that can be dragged and dropped. I'm going to attach a <code>String</code> and a <code>MyObject</code>; the <code>String</code> can be retrieved while the <code>MyObject</code> cannot.<br>
  Note, I'm aware there are simpler ways to attach simple strings to the dragboard, this is just an example to show that <code>MyObject</code> should be retrievable from the dragboard also.</p>
  
  <p>Here I create a custom component which can be dragged.
  When it is dragged, two objects are attached to the dragboard: a <code>MyObject</code> and a <code>String</code>.</p>
  
  <pre><code>class ToolboxItem
      extends Label {
  
      setOnDragDetected(new EventHandler[MouseEvent] {
          def handle(event: MouseEvent) {
          val dragboard = startDragAndDrop(TransferMode.COPY)
          val content = new ClipboardContent()
          content.put(DnDTarget.DndString, "sean is cool")
          content.put(DnDTarget.DndObject, new MyObject)
          dragboard.setContent(content)
          event.consume()
      }
  
  }
  </code></pre>
  
  <p><code>MyObject</code> is super simple for the sake of getting going:</p>
  
  <pre><code>class MyObject
      extends Serilaizable
  </code></pre>
  
  <p>Now, when I receive a drag event, I want to grab this data from the event.<br>
  (also, here I define my <code>DataFormat</code>)</p>
  
  <pre><code>object DnDTarget {
  
      val DndString = new DataFormat("my.custom.dnd.string")
      val DndObject = new DataFormat("my.custom.dnd.object")
  
  }
  
  trait DnDTarget
      extends Node {
  
      setOnDragOver(new EventHandler[DragEvent]() {
          def handle(event: DragEvent) {
              if (valid(event)) {
                  val dragboard = event.getDragboard
                  val myString = dragboard.getContent(DnDTarget.DndString)
                  val myObject = dragboard.getContent(DnDTarget.DndObject)
                  myString.asInstanceOf[String]   // no problem
                  myObject.asInstanceOf[MyObject] // throws exception
                  event.acceptTransferModes(TransferMode.COPY)
              }
              event.consume()
          }
      })
  }
  </code></pre>
  
  <p>Ok, so, calling <code>myString.asInstanceOf[String]</code> works fine, I got my <code>String</code> back. However, <code>myObject.asInstanceOf[MyObject]</code> throws a <code>ClassCastException</code> saying:</p>
  
  <blockquote>
    <p>Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.nio.HeapByteBuffer cannot be cast to com.example.MyObject</p>
  </blockquote>
  
  <p>Note: I am <em>not</em> using ScalaFX here, vanilla JavaFX in Scala</p>