JavaFX launch another application

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

I've been smashing my head with JavaFx...

This works for when there's no instances of an application running:

public class Runner {      public static void main(String[] args) {         anotherApp app = new anotherApp();         new Thread(app).start();     }  }  public class anotherApp extends Application implements Runnable {      @Override     public void start(Stage stage) {     }      @Override     public void run(){         launch();     } } 

But if I do new Thread(app).start() within another application I get an exception stating that I can't do two launches.

Also my method is called by an observer on the other application like this:

@Override public void update(Observable o, Object arg) {     // new anotherApp().start(new Stage());             /* Not on FX application thread; exception */      // new Thread(new anotherApp()).start();             /* java.lang.IllegalStateException: Application launch must not be called more than once */ } 

It's within a JavaFX class such as this:

public class Runner extends Applications implements Observer {      public static void main(String[] args) {         launch(args);     }      @Override     public void start(Stage stage){     //...code...//     }     //...methods..//     //...methods..//      @Override     public void update(Observable o, Object arg) {     //the code posted above//     } } 

I tried using ObjectProperties with listeners but it didn't work. I need to get this new stage running from within the update method from java.util.observer in some way.

Any suggestions are welcomed. Thanks.

回答1:

Application is not just a window -- it's a Process. Thus only one Application#launch() is allowed per VM.

If you want to have a new window -- create a Stage.

If you really want to reuse anotherApp class, just wrap it in Platform.runLater()

@Override public void update(Observable o, Object arg) {     Platform.runLater(new Runnable() {        public void run() {                         new anotherApp().start(new Stage());        }     }); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!