Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch JavaFX application from another class

Tags:

javafx-8

I need to start a javafx Application from another "container" class and call functions on the Application, but there doesn't seem to be any way of getting hold of a reference to the Application started using the Application.launch() method. Is this possible? Thanks

like image 754
Oli Avatar asked Sep 16 '14 16:09

Oli


2 Answers

Suppose this is our JavaFX class:

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage;  public class OKButton extends Application {      @Override     public void start(Stage stage) {         Button btn = new Button("OK");         Scene scene = new Scene(btn, 200, 250);         stage.setTitle("OK");         stage.setScene(scene);         stage.show();     } } 

Then we may launch it from another class like this:

import javafx.application.Application;  public class Main {     public static void main(String[] args) {         Application.launch(OKButton.class, args);     } } 
like image 190
ThisClark Avatar answered Sep 17 '22 17:09

ThisClark


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.

like image 23
Boomah Avatar answered Sep 21 '22 17:09

Boomah