/* * --------------- * CursorExample.java * --------------- */ package cursorexample; import javafx.application.Application; import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Cursor; import javafx.scene.ImageCursor; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * This class provides a quick demonstration on * implementing different cursor styles in JavaFX. * * @author Nathan Howard */ public class CursorExample extends Application { @Override public void start(Stage primaryStage) { /** * Setup Button. */ Button btn = new Button(); btn.setText("Count Iterations"); /** * Setup Scene. */ StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); btn.setOnMouseEntered(new EventHandler() { public void handle(MouseEvent me) { scene.setCursor(Cursor.HAND); //Change the cursor to a Hand } }); btn.setOnMouseExited(new EventHandler() { public void handle(MouseEvent me) { scene.setCursor(Cursor.CROSSHAIR); //Change the cursor to a Crosshair } }); /** * Add Listeners. */ btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { /** * Only for JavaFX Thread, use runLater if on main. */ Task task = new Task() { @Override protected Integer call() throws Exception { int iterations; scene.setCursor(Cursor.WAIT); //Change cursor to wait style for (iterations = 0; iterations < 100000; iterations++) { System.out.println("Iteration " + iterations); } scene.setCursor(Cursor.DEFAULT); //Change cursor to default style return iterations; } }; Thread th = new Thread(task); th.setDaemon(true); th.start(); } }); Image image = new Image("batman.png"); scene.setCursor(new ImageCursor(image)); /** * Finalise Stage. */ primaryStage.setTitle("Cursor Example"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }