I have a JavaFX application that I can't seem to change the Cursor in and was hoping someone could take a look at my code and maybe let me know what I'm doing wrong.
Basic flow of what's happening:
Right before I create the task to go and hit the REST API, I'm calling:
myPrimaryStage.getScene().setCursor(Cursor.WAIT);
Then, upon completion of the task I'm calling:
this.myController.getMyPrimaryStage().getScene().setCursor(Cursor.DEFAULT);
For some reason though, I never actually see the cursor change. The application works just fine, but the cursor always stays as the default.
Anybody got any suggestions?
NOTE: I added in the for-loop just to eat up some time so I could see if maybe the task was just completing really fast, but even with that running, I never see the cursor change. So I must be doing something wrong.
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import logic.MyController;
public class Main extends Application {
private AnchorPane rootLayout;
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainScreen.fxml"));
rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
//Store the primaryStage in my controller
MyController controller = loader.getController();
controller.setMyPrimaryStage(primaryStage);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package logic;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class MyController {
@FXML private Button myButton;
@FXML private TextArea myTextArea;
private Stage myPrimaryStage;
@FXML
public void initialize() {
myButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
myPrimaryStage.getScene().setCursor(Cursor.WAIT);
myButton.setDisable(true);
CreateOWMWorker myWorker = new CreateOWMWorker(MyController.this);
new Thread(myWorker).start();
}
});
}
public Button getMyButton(){
return this.myButton;
}
public TextArea getMyTextArea(){
return this.myTextArea;
}
public Stage getMyPrimaryStage() {
return myPrimaryStage;
}
public void setMyPrimaryStage(Stage primaryStage) {
this.myPrimaryStage = primaryStage;
}
}
package logic;
import javafx.concurrent.Task;
import javafx.scene.Cursor;
import org.json.JSONObject;
public class CreateOWMWorker extends Task{
private MyController myController = null;
public CreateOWMWorker(MyController myController){
this.myController = myController;
}
@Override
protected Object call() throws Exception {
OpenWeatherClient myTest = new OpenWeatherClient();
Object someResponse = null;
try {
someResponse = myTest.retrieveWeatherData("weather?q=London,uk");
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 1000000; i++) {
System.out.println("Cycle #: " + i);
}
return someResponse;
}
protected void done(){
super.done();
try {
JSONObject someResponse = (JSONObject) get();
if (null != someResponse) {
JSONObject someObject = someResponse.getJSONObject("wind");
this.myController.getMyTextArea().setText(someObject.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
this.myController.getMyPrimaryStage().getScene().setCursor(Cursor.DEFAULT);
this.myController.getMyButton().setDisable(false);
}
}
}
Did you tried to change the cursor of your root element?
instead of this
myPrimaryStage.getScene().setCursor(Cursor.WAIT);
try this:
myPrimaryStage.getScene().getRoot().setCursor(Cursor.WAIT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With