Last active
August 30, 2021 16:54
-
-
Save daveyostcom/c09e7bac42d8db12314ec2920ab89922 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shop.examples; | |
import javafx.application.Application; | |
import javafx.beans.Observable; | |
import javafx.scene.Cursor; | |
import javafx.scene.Scene; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.Pane; | |
import javafx.stage.Stage; | |
public class ChangingCursor extends Application { | |
Pane root; | |
@Override | |
public void start(Stage stage) { | |
root = new Pane(); | |
root.setOnMousePressed (this::onMousePressed ); | |
root.setOnMouseReleased(this::onMouseReleased); | |
root.setOnMouseEntered (this::onMouseEntered ); | |
root.setOnMouseExited (this::onMouseExited ); | |
root.cursorProperty().addListener(this::changedCursor); | |
stage.setScene(new Scene(root, 300, 200)); | |
stage.show(); | |
} | |
void onMousePressed (MouseEvent e) { | |
System.out.println("pressed" ); root.setCursor(Cursor.CROSSHAIR); | |
} | |
void onMouseReleased(MouseEvent e) { | |
System.out.println("released"); root.setCursor(Cursor.DEFAULT ); | |
} | |
void onMouseEntered (MouseEvent e) { | |
System.out.println("entered" ); root.setCursor(Cursor.HAND ); | |
} | |
void onMouseExited (MouseEvent e) { | |
System.out.println("exited" ); | |
// root.setCursor(Cursor.CLOSED_HAND); // has no effect | |
} | |
private void changedCursor(Observable ob, Cursor o, Cursor n) { | |
System.out.println("changedCursor " + o + " " + n); | |
} | |
public static void main(String[] args) { launch(args); } | |
} | |
/* Behavior on macOS 11.5.2 JavaFX 15.0.2 | |
entered | |
changedCursor null HAND | |
exited | |
// stays HAND | |
entered | |
// stays HAND | |
pressed | |
changedCursor HAND CROSSHAIR | |
released | |
changedCursor CROSSHAIR DEFAULT | |
pressed | |
changedCursor DEFAULT CROSSHAIR | |
exited | |
// cursor changed to <–>, with no change event | |
released | |
changedCursor CROSSHAIR DEFAULT <–– wrong; was <–> and stayed <–> | |
entered | |
changedCursor DEFAULT HAND <–– wrong; was <–> | |
pressed | |
changedCursor HAND CROSSHAIR | |
exited | |
// cursor changed to <–>, with no change event | |
entered | |
changedCursor CROSSHAIR HAND <–– wrong; was <–> and stayed <–> | |
released | |
changedCursor HAND DEFAULT <–– wrong; was <–> and stayed <–> | |
// Here if you move the cursor, | |
// it does change to DEFAULT, with no change event. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StackOverflow question