JavaFX launch another application
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()
@Overridepublic void update(Observable o, Object arg) { Platform.runLater(new Runnable() { public void run() { new anotherApp().start(new Stage()); } });}
I did a constructor of another JFX class in Main class AnotherClass ac = new AnotherClass();
and then called the method ac.start(new Stage);
. it worked me fine. U can put it either in main() or in another method. It does probably the same thing the launch(args) method does
Wanted to provide a second answer because of one caveat of using
Application.start(Stage stage).
The start method is called after the init method has returned
If your JavaFX application has Override Application.init() then that code is never executed. Neither is any code you have in the second application main method.
Another way to start a second JavaFX application is by using the ProcessBuilder API to start a new process.
final String javaHome = System.getProperty("java.home"); final String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; final String classpath = System.getProperty("java.class.path"); final Class<TestApplication2> klass = TestApplication2.class; final String className = klass.getCanonicalName(); final ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className); final Button button = new Button("Launch"); button.setOnAction(event -> { try { Process process = builder.start(); } catch (IOException e) { e.printStackTrace(); } });