Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressed state gets stuck after drag and drop

Tags:

java

javafx

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.

remains pressed

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

  • detect mouse released on circle - this is not triggered
  • manually clear the psuedo class with circle.pseudoClassStateChanged(PseudoClass.getPseudoClass("pressed"), false); - this works for CSS styling but circle does not go back to pressed again.
  • different platforms - Redhat 7, Redhat 8, Windows 10, JavaFX 11.0.2 / 17.0.2
like image 928
Adam Avatar asked Nov 29 '25 17:11

Adam


1 Answers

This is indeed a bug in JavaFX. I have raised as JDK-8307098

like image 114
Adam Avatar answered Dec 01 '25 08:12

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!