Skip to content

Instantly share code, notes, and snippets.

@daveyostcom
Last active August 30, 2021 16:54
Show Gist options
  • Save daveyostcom/c09e7bac42d8db12314ec2920ab89922 to your computer and use it in GitHub Desktop.
Save daveyostcom/c09e7bac42d8db12314ec2920ab89922 to your computer and use it in GitHub Desktop.
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.
*/
@daveyostcom
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment