I believe I've discovered an issue with the pressed state of a JavaFX node after a drag and drop operation. The pressed state gets "stuck" as true after the drop.
The following example demonstrates the issue. Try and drag the black circle to anywhere in the window, observe it remains red after the drop.
I've visually highlighted the issue by coloring the circle red whilst pressed. This is usually done with :pressed CSS pseudo class, but I've done it like this for sake of a self contained example.

import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class StuckPressedState extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Circle circle = new Circle(100, 100, 20);
circle.setOnDragDetected(event -> {
Dragboard db = circle.startDragAndDrop(TransferMode.ANY);
ClipboardContent clipBoardContent = new ClipboardContent();
clipBoardContent.putString("foo");
db.setContent(clipBoardContent);
});
circle.pressedProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("Pressed " + newValue);
if (Boolean.TRUE.equals(newValue)) {
circle.setFill(Color.RED);
} else {
circle.setFill(Color.BLACK);
}
});
circle.setOnMouseReleased(event -> {
System.out.println(event);
});
Pane root = new Pane(circle);
root.setOnDragOver(event -> {
event.acceptTransferModes(TransferMode.ANY);
});
root.setOnDragDropped(event -> {
System.out.println("Dropped " + event.getDragboard().getContent(DataFormat.PLAIN_TEXT));
circle.pseudoClassStateChanged(PseudoClass.getPseudoClass("pressed"), false);
event.setDropCompleted(true);
});
root.setPrefSize(200, 200);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
Things I've tried
circle.pseudoClassStateChanged(PseudoClass.getPseudoClass("pressed"), false); - this works for CSS styling but circle does not go back to pressed again.This is indeed a bug in JavaFX. I have raised as JDK-8307098
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With