JavaFx从后台线程更新/加载新的FXML GUI

时间:2015-10-05 08:53:27

标签: java multithreading javafx background

我正在尝试更新显示游戏的UI线程。后台线程必须运行整个游戏,因为它连接到服务器并且必须始终从服务器轮询信息。我已经尝试过platform.runlater,任务和服务,但我似乎做了一些非常错误的事情。通常后台不知道来自应用程序线程的数据,因此我得到IllegalMonitorStateExceptions或NullPointerExceptions用于" rootLayout" BorderPane。这是我想要做的粗略结构。 首先,我创建了类GUI的对象,然后调用方法" startApplication()"。 谢谢你的帮助。

  public class GUI extends Controller implements Runnable {

         public GUI(String ip, int port, String name) throws IOException {
                    super(ip, port, name);
                    this.name = name;
                    this.runs = false;

                    // Pass model and this class to the view class App
                    application = new App();
                    application.gainAccess(model, this);`
                    }

                    public void startApplication() {
                         application.main(null);
                    }   

                    public void run () {
                         //Calculations in Background Thread
                         Platform.runLater(new Runnable() {
                             @Override public void run() {
                                application.showMainWindow();

                             }
                        });
                         //more Calculations
                    }

            }
}


public class App extends Application implements Runnable {
    public static void main(String[] args) {
        launch(args); // calls start(primaryStage)
    }    

    public void start(Stage primaryStage) throws Exception {
        System.out.println("start: " + gui);

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("TicketToRide");
        primaryStage.setMaximized(true);
        this.initRootWindow();

        this.showLoadingScreen();
        primaryStage.setFullScreen(false);
    }

    private void showLoadingScreen() {
        try {
           FXMLLoader loader = new FXMLLoader();
           loader.setLocation(App.class.getResource("LoadingWindow.fxml"));
           AnchorPane loadingWindow = (AnchorPane) loader.load();

           rootLayout.setCenter(loadingWindow);

           // Give the controller access to this class and with that the model
          LoadingWindowController loadingController =                loader.getController();
          loadingController.setApp(this);
       } catch (IOException e) {
          e.printStackTrace();
       }
   }

   public void initRootWindow() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(App.class.getResource("RootWindow.fxml"));
        rootLayout = (BorderPane) loader.load();

        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();

        // Give the controller access to this class and with that the model
        RootWindowController rootController = loader.getController();
        rootController.setApp(this);    
    } catch (IOException e) {
        e.printStackTrace();
    }
 }

 public void showMainWindow() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(App.class.getResource("MainWindow.fxml"));
            AnchorPane mainWindow = (AnchorPane) loader.load();

            rootLayout.setCenter(mainWindow);

            // Give the controller access to this class and with that the model
            MainWindowController mainController = loader.getController();
            mainController.setApp(this);
           // mainController.drawMap();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


 }

出于某种原因" rootlayout"是空的。

堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at gui.implemenation.App.initRootWindow(App.java:94)
    at gui.implemenation.App.showEndingWindow(App.java:193)
    at de.unisaarland.sopra.zugumzug.GUI$1.run(GUI.java:48)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/12559810.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/2990725.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/12617740.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案
相关问题