I'm trying to change the cursor to indicate that the program is busy. It seems like this should be pretty simple, but I haven't been able to get it to work correctly and after searching for several hours, I haven't come up with the solution. Here's what I'm trying to do.
public class ButtonTest extends Application {
@Override
public void start(Stage primaryStage) {
final StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 200, 150));
Button button = new Button();
button.setText("Button");
root.getChildren().add(button);
primaryStage.show();
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Set the cursor to the wait cursor.
root.setCursor(Cursor.WAIT);
// Do some work.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Set the cursor back to the default.
root.setCursor(Cursor.DEFAULT);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
If I comment out the setCursor(Cursor.DEFAULT), after I press the button it waits 5 seconds then changes the cursor to the wait cursor. So it seems like it waiting until after the sleep, then executing setCursor(Cursor.WAIT), immediately followed by the setCursor(Cursor.DEFAULT). What am I missing?