我需要根據客戶端輸入更新我的GUI。調用我的控制器類的方法,從後臺任務的作品。但它不能更新GUI,因爲它不是JavaFX應用程序線程。請幫助。從線程更新JavaFX場景圖形
我嘗試了許多相關的Q & A,但我仍然感到困惑。
我應該使用Platform. runLater
還是Task
?
這裏是我的課,我創建controller
類
public class FactoryClass {
public static Controller_Gui1 createGUI() {
FXMLLoader fxLoader = new FXMLLoader();
fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
AnchorPane anchorPane = null;
try {
anchorPane = (AnchorPane) fxLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader
.getController();
Scene scene = new Scene(anchorPane);
//System.out.println(scene);
controller_Gui1.setScene(scene);
return controller_Gui1;
}
}
Controller類
@FXML
Button B1 = new Button();
@FXML
public void handleButton1() {
B1.setDisable(true);
}
應用類
public class MainApp_Gui1 extends Application {
Controller_Gui1 cGui;
@Override
public void start(Stage primaryStage) throws Exception {
initScene(primaryStage);
primaryStage.show();
System.out.println("asdasd");
SceneSetting sceneSetting = new SceneSetting();
//handleEvent();
System.out.println("after");
sceneSetting.setSceneAfter();
System.out.println("after2");
}
// creating scene
private void initScene(Stage primaryStage) throws IOException {
primaryStage.setScene(getScene(primaryStage));
}
public Scene getScene(Stage primaryStage) {
Controller_Gui1 cGui;
cGui = FactoryClass.createGUI();
return cGui.getScene();
}
public void ExcessFromOutside() {
Platform.runLater(new Runnable() {
public void run() {
System.out.println(Platform.isFxApplicationThread());
cGui.handleButton1();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
我的一個實例想從另一個線程調用ExcessFromOutside()
方法。
我試圖更新的GUI 這是我的應用程序類
public class MainAppGui1 extends Application {
Controller_Gui1 controller_Gui1;
@Override
public void start(Stage primaryStage) throws Exception {
initScene(primaryStage);
primaryStage.show();
}
// creating scene
public void initScene(Stage primaryStage) throws IOException {
FXMLLoader fxLoader = new FXMLLoader();
fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
AnchorPane anchorPane=new AnchorPane();
anchorPane = (AnchorPane) fxLoader.load();
Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader.getController();
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
}
@FXML
public void ExcessFromOutside()
{
Platform.runLater(new Runnable() {
@Override
public void run() {
System.out.println("called atleast");
controller_Gui1.handleButton1();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
一個空指針異常,這是從那裏我試圖更新GUI類
public class Hudai {
public static void main(String args[]) throws InterruptedException
{
new Thread()
{
public void run()
{
MainAppGui1.main(null);
}
}.start();
Thread.sleep(5000);
MainAppGui1 m = new MainAppGui1();
m.ExcessFromOutside();
}
}
你可以添加一些代碼來告訴我們什麼不適合你嗎? – ItachiUchiha 2015-03-25 09:51:55
我應該使用任務嗎?這是我的第一篇文章..所以考慮... – xoxis 2015-03-25 10:29:32
你爲什麼使用'MainAppGui1.main(null);'? – ItachiUchiha 2015-03-30 08:32:05