title: After JavaFX drag and drop I need to click twice to close window
body: <p>In a simple drag and drop example program where strings can be dragged from a list cell to a text field, everything works fine, except that after a drop, the mouse does not behave normally: for example, I have to click twice on the window close button for the window to close.</p>
  
  <p>However, in the following cases, everything works fine:</p>
  
  <ul>
  <li>Using a label instead of a text field (but the problem remains with a text area)</li>
  <li>Dropping a string into the text field from another application</li>
  <li>Dragging the string from the list cell onto another application</li>
  </ul>
  
  <p>Clicking anywhere in the window also 'frees' the close button - even outside the list or the text field.</p>
  
  <p><strong>Edited</strong>: Below is a minimal working example (class <code>DnD</code> in package <code>dnd</code>). Note in particular the drag and drop handlers used on the field and on the list cell. To reproduce the problem: run the program, drag Banana from the list to the text field and then try to close the window. In my case I have to click twice.</p>
  
  <pre><code>package dnd;
  
  import javafx.application.Application;
  import javafx.geometry.Insets;
  import javafx.scene.Scene;
  import javafx.scene.control.ListView;
  import javafx.scene.control.TextField;
  import javafx.scene.control.cell.TextFieldListCell;
  import javafx.scene.input.ClipboardContent;
  import javafx.scene.input.Dragboard;
  import javafx.scene.input.TransferMode;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  public class Dnd extends Application {
  
      @Override
      public void start(Stage stage) {
          stage.setScene(new Scene(new DndPanel(), 200, 300));
          stage.setTitle("Drag and drop");
          stage.setResizable(false);
          stage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
      public static class DndPanel extends VBox {
  
          public DndPanel() {
              super(10);
              TextField field = new TextField();
              ListView&lt;String&gt; list = new ListView&lt;&gt;();
              list.setCellFactory(Dnd.DnDCell::new);
              list.getItems().addAll("Apple", "Pear", "Banana", "Grapes", "Peach", "Pineapple");
              setPadding(new Insets(10));
              getChildren().addAll(field, list);
  
              field.setOnDragOver(ev -&gt; {
                  if (ev.getDragboard().hasString()) {
                      ev.acceptTransferModes(TransferMode.COPY);
                  }
                  ev.consume();
              });
  
              field.setOnDragDropped(ev -&gt; {
                  field.setText(ev.getDragboard().getString());
                  ev.setDropCompleted(true);
                  ev.consume();
              });
  
          }
  
      }
  
      public static class DnDCell extends TextFieldListCell&lt;String&gt; {
          public DnDCell(ListView&lt;String&gt; listView) {
              setOnDragDetected(ev -&gt; {
                  Dragboard dragboard = startDragAndDrop(TransferMode.COPY);
                  ClipboardContent cc = new ClipboardContent();
                  cc.putString(listView.getItems().get(getIndex()));
                  dragboard.setContent(cc);
                  ev.consume();
              });
          }
      }
  }
  </code></pre>
  
  <p>Tested on JavaFX 11.0.2 from Gluon on Ubuntu Linux 18.04 with additional
  JVM runtime option <code>-Djdk.gtk.version=2</code>.</p>