Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX setCursor( ) not working

Tags:

java

javafx

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:

  1. Click button
  2. Execute a separate task to go retrieve data from a REST API.
  3. Display retrieved data in TextArea component.

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.

Main.java

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);
    }
}

MyController.java

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;
    }
}

CreateOWMWorker.java

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);
        }
    }
}
like image 820
Chiefwarpaint Avatar asked Oct 20 '22 07:10

Chiefwarpaint


1 Answers

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);
like image 147
Marcel Avatar answered Oct 23 '22 04:10

Marcel