我有一个场景,在执行每个测试之后,我想启动一个Javafx应用程序窗口。
这样做的时候,我发现了以下异常 java.lang.IllegalStateException:不在FX应用程序线程上; currentThread =主要 在语句stage.setScene(scene);
请在下面找到我的代码
package com.comcast.ui;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.apache.log4j.Logger;
import com.comcast.main.PopulateMethod;
import com.comcast.main.XMLCreator;
import com.comcast.ui.controller.PopupUIController;
import com.comcast.ui.controller.UIController;
import com.comcast.ui.data.TestData;
import com.comcast.ui.data.TestMethod;
import com.sun.javafx.application.LauncherImpl;
import com.sun.javafx.tk.Toolkit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class PopupUI extends Application {
static Logger log = Logger.getLogger(PopupUI.class);
private static String startUpFile = "PopupUI.fxml";
private static String header = "Interactive API Pop up";
private static int screenWidth = 1000;
private static int screenHeight = 1000;
private static Pane view = null;
private static Scene scene = null;
private static LinkedHashMap<String, String> currentStepHashPopUp;
public static int clickButton = 0;
private static boolean launchPopupUI;
public static Stage stage;
private static PopupUIController controller = null;
public static boolean isStageVisible = false;
public static final int btnContinueClick = 1;
public static final int btnAbortClick = 100;
private static PopupUI popupUIClassInstance;
public static Thread thread;
@Override
public void init() throws Exception {
// Do some heavy lifting
}
@Override
public void start(Stage primaryStage) {
stage = primaryStage;
// Platform.setImplicitExit(false);
// popupUIClassInstance = this;
// thread = Thread.currentThread();
loadStage(primaryStage);
}
// });
// }
// public static void main(String[] args) {
//// launch(args);
//// LauncherImpl.launchApplication(PopupUI.class, args);
// Application.launch();
// Platform.setImplicitExit(false);
//
// while (clickButton < 1) {
// }
// System.out.println("Program Ends here");
//
// }
public void startStandAlonetApplication(String[] args,
LinkedHashMap<String, String> currentStepHash) {
currentStepHashPopUp = currentStepHash;
launch(args);
}
public static void hide() {
if (controller == null) {
log.debug("controller is null");
return;
}
stage.hide();
isStageVisible = false;
}
public static void show() {
if (controller == null) {
log.debug("controller is null");
return;
}
Platform.runLater(() -> {
stage.show();
});
isStageVisible = true;
}
public void loadStage(Stage stage) {
try {
FXMLLoader loader = new FXMLLoader(PopupUI.class.getResource("PopupUI.fxml"));
// Create a controller instance
// currentStepHashPopUp = new LinkedHashMap<String, String>();
controller = new PopupUIController(currentStepHashPopUp);
// Set it in the FXMLLoader
loader.setController(controller);
// loader.load();
view = loader.load();
} catch (Exception e) {
e.printStackTrace();
}
if (startUpFile.equalsIgnoreCase("PopupUI.fxml"))
scene = new Scene(view, 1500, 1000);
else
scene = new Scene(view, screenWidth, screenHeight);
Button btnContinue = (Button) scene.lookup("#btnContinue");
Button btnAbort = (Button) scene.lookup("#btnAbort");
EventHandler<ActionEvent> btnContinueHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Continue Button Click");
// btnContinue.setDisable(true);
// stage.hide
// Platform.exit();
hide();
clickButton = btnContinueClick;
}
};
;
btnContinue.setOnAction(btnContinueHandler);
EventHandler<ActionEvent> btnAbortHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Abort Button Click");
// stage.hide();
// Platform.exit();
hide();
// btnAbort.setDisable(true);
clickButton = btnAbortClick;
}
};
btnAbort.setOnAction(btnAbortHandler);
stage.setTitle(header);
stage.setScene(scene);
stage.show();
}
}
这是我在测试结束时尝试调用此应用程序的方式:
PopupUI popupUI = new PopupUI();
if(PopupUI.stage==null) {
popupUI.startStandAlonetApplication(args, currentStepHashUIPrompt);
}else {
popupUI.start(PopupUI.stage);
}
如何避免此错误?请帮忙吗?