Issue
Using JavaFX 8 I'm experiencing a specific drag-and-drop problem:
The confirmation popup after dropping gets the icon stuck on screen when a drag is released, even overlaying the Alert dialog itself like so:
(source: image.ibb.co)
The text "copy" and the icon remain stuck until user closes the popup.
This is the minimal code to reproduce the problem. To test, run this program and drag any file (ex. from Desktop) into the app window:
public class Main extends Application {
private Parent root = new VBox();
private void onDragOver(DragEvent dragEvent) {
if (dragEvent.getDragboard().hasFiles()) {
dragEvent.acceptTransferModes(TransferMode.COPY);
}
}
private void isUserSure() {
Alert alert = new Alert(Alert.AlertType.WARNING,"",ButtonType.OK);
alert.showAndWait();
}
@Override
public void start(Stage primaryStage) {
root.setOnDragOver((event) -> onDragOver(event));
root.setOnDragDropped((event) -> isUserSure());
primaryStage.setTitle("ghost demo");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Solution
I solved it myself by doing the following:
Knowing that Modal Dialogs (Warning popups etc.) essentially "block" every other Stage via a call to showAndwait()
the problem must be that this also blocks the dragEvent
from finishing in the onDragDropped method (set via lambda in setOnDragDropped
).
Make sure you wrap the call to your popup method and the stuff that is to happen to the actual dropped items in a Platform.runLater()
This will let the dragEvent
stuff finish first. Observe the change I made in the following line of method start
:
root.setOnDragDropped((event) -> Platform.runLater(() -> isUserSure()));
Make sure you don't wrap more than you have to, otherwise the items in the Dragboard
will go out of scope. Extraction of needed items from Dragboard
has to happen at the time of drop, and not inside runLater()
Answered By - Pisces
Answer Checked By - David Goodson (JavaFixing Volunteer)