Javafx Launch Another Application

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()

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

JavaFX launch Application standalone OR from another application

Can you do something like this:

public class MainApp extends Application {

private Parent uiContent ;

public static final double DEFAULT_WIDTH = 800 ;
public static final double DEFAULT_HEIGHT = 600 ;

public Parent getContent() {
if (uiContent == null) {
uiContent = initializeUI();
}
return uiContent ;
}

public Scene createScene() {
return new Scene(getContent(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
}

public void initializeAndShowStage(Stage stage) {
stage.setScene(createScene());
stage.show();
}

private Parent initializeUI() {
// probably wise to check we are on the FX Application thread here...
Pane root = ... ;
// build ui....
return root ;
}

@Override
public void start(Stage primaryStage) throws Exception {
initializeAndShowStage(primaryStage);
}

public static void main(String[] args) {
launch(args);
}
}

And

public class UpdaterApp extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
// whatever you need to launch the updater app here...

}

// invoke from the FX Application Thread to "start" main app:
private void showMainApp(Stage stage) {
MainApp app = new MainApp();
app.initializeAndShowStage(stage);
}

private void showMainApp() {
showMainApp(new Stage());
}

public static void main(String[] args) {
launch(args);
}
}

This would be the far preferred approach. If you have requirements that force you to call main, then you could try something like this:

public class MainApp extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
// .... build UI etc
}

public static void main(String[] args) throws Exception {
if (Platform.isFXApplicationThread()) {
Stage someStage = new Stage();
MainApp app = new MainApp();
app.start(stage);
} else {
launch(args);
}
}
}

Then your updater app can just call MainApp().main(new String[0])); from the FX Application Thread.

This feels like a bit of a hack though.

Launch JavaFX application from another class

I had the same problem as this and got round it using this hack:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.concurrent.CountDownLatch;

public class StartUpTest extends Application {
public static final CountDownLatch latch = new CountDownLatch(1);
public static StartUpTest startUpTest = null;

public static StartUpTest waitForStartUpTest() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return startUpTest;
}

public static void setStartUpTest(StartUpTest startUpTest0) {
startUpTest = startUpTest0;
latch.countDown();
}

public StartUpTest() {
setStartUpTest(this);
}

public void printSomething() {
System.out.println("You called a method on the application");
}

@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);

Label label = new Label("Hello");
pane.setCenter(label);

stage.show();
}

public static void main(String[] args) {
Application.launch(args);
}
}

and then the class you are launching the application from:

public class StartUpStartUpTest {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
javafx.application.Application.launch(StartUpTest.class);
}
}.start();
StartUpTest startUpTest = StartUpTest.waitForStartUpTest();
startUpTest.printSomething();
}
}

Hope that helps you.

Starting a second JavaFX Application

If you want to execute another JavaFX application in the same JVM you can just create instance of it, manually create Stage and call Application#start()

public void runAnotherApp(Class<? extends Application> anotherAppClass) throws Exception {
Application app2 = anotherAppClass.newInstance();
Stage anotherStage = new Stage();
app2.start(anotherStage);
}

N.B.: it wouldn't work if you use special features of standard initialization in anotherApp, e.g. Application.init() or Application.getParameters()

JavaFx launch application and continue

I'm not sure what you're trying to do, but Application.launch also waits for the application to finish which is why you're not seeing the output of line 3 immediately. Your application's start method is where you want to do your setup. See the API docs for the Application class for more information and an example.

Edit: if you want to run multiple JavaFX apps from a main thread, maybe this is what you need:

public class AppOne extends Application
{
@Override
public void start(Stage stage)
{
Scene scene = new Scene(new Group(new Label("Hello from AppOne")), 600, 400);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args)
{
System.out.println("Starting first app");
Platform.runLater(() -> {
new AppOne().start(new Stage());
});
System.out.println("Starting second app");
Platform.runLater(() -> {
new AppTwo().start(new Stage());
});
}
}

public class AppTwo extends Application
{
@Override
public void start(Stage stage)
{
Scene scene = new Scene(new Group(new Label("Hello from AppTwo")), 600, 400);
stage.setScene(scene);
stage.show();
}
}

This runs multiple apps from the main thread by running their start methods on the JavaFX thread. However, you will lose the init and stop lifecycle methods because you're not using Application.launch.

how to launch javafx application from another class with default constructor?

Don't put the launch method call in the constructor; It is a blocking method call and will not return until the application exits.

The GUI default constructor will be called by the Application.launch method and you do not need to (and cannot) pass your instance to application launcher. If you really want to call from the main method in another class, you may use Application.launch(Gui.class, args) directly.



Related Topics



Leave a reply



Submit