我有一个JavaFX应用程序,我似乎无法更改Cursor,并希望有人可以查看我的代码,也许让我知道我做错了什么。
正在发生的事情的基本流程:
在创建任务然后点击REST API之前,我正在调用:
myPrimaryStage.getScene().setCursor(Cursor.WAIT);
然后,完成我正在调用的任务后:
this.myController.getMyPrimaryStage().getScene().setCursor(Cursor.DEFAULT);
出于某种原因,我从未真正看到光标发生变化。应用程序工作正常,但光标始终保持默认状态。
有人有任何建议吗?
注意:我在for-loop中添加只是为了吃掉一些时间,所以我可以看到这个任务是否真的很快完成,但即使运行,我也从未看到光标发生变化。所以我一定做错了。
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);
}
}
}
答案 0 :(得分:2)
您是否尝试更改根元素的光标?
而不是
myPrimaryStage.getScene().setCursor(Cursor.WAIT);
试试这个:
myPrimaryStage.getScene().getRoot().setCursor(Cursor.WAIT);