How JavaFX starts exactly

I just wrote an application JavaFX

and realized that I really don't understand how it actually starts.

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = new FXMLLoader(this.getClass().getResource("view.fxml")).load();
    primaryStage.setTitle("Dice Roller");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

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

      

This is the template given to me by Intellij. My entry point will be the main one where the only thing that starts is launch(args)

. I tried digging around in the Application class but didn't find anything that would indicate the start method was running. How is it even started? Since it JavaFX

has its own thread, I am assuming that the thread is created in the start method and the main thread does not return from the moment it starts until you call Platform.exit

or just close all windows. All of this is just too abstract for me now. Can someone explain to me how it all fits together?

+3


source to share


2 answers


Below is an example of your main application class. I named it "HelloApp" because I needed a name. I also changed launch

to Application.launch

, this is the same but less confusing (see explanations below).

Please note, however (thanks to @Cypher in the comments for this) that the good old method is not needed to run a JavaFX application . If you omit the method from the bottom, compile it and run it with , it will work and might be a little less confusing :) You can still handle command line arguments without , since they are passed to an options object, which you can get from yours with . java

public static void main(String[] args)


main

java HelloApp


main

Application

getParameters()

My IDE also allows you to run Application

without main

and apparently most of them.

Speaking of which, let's see what's going on here in your case:



import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class HelloApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = new FXMLLoader(this.getClass().getResource("view.fxml")).load();
        primaryStage.setTitle("Dice Roller");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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

}

      

  • HelloApp.main()

    causes launch()

  • launch

    is a static method in the class Application

    that you extend, so you can use directly launch()

    , but it is recommended to use the class name when calling static methods, so I replaced the above with Application.launch

    . Some IDEs and linters warn you that "a java static method must be statically accessible" if you don't.
  • Within the method launch

    , the JavaFX runtime (below "JavaFX") determines which class is the caller (using Thread.currentThread().getStackTrace()

    , but this is just an interesting implementation detail). The caller class is expected to bejavafx.application.Application


    - when working without the main

    above doesn't matter, and JavaFX starts here. -sub>
  • JavaFX creates an application thread to run the application's launch method, handle input events, and execute animation timing.
  • JavaFX creates an instance of the specified Application class
  • JavaFX calls init()

    (which you can override)
  • JavaFX calls start()

    (which you must implement yourself) from "JavaFX Application Thread"

The rest of the lifecycle is described in the Application javadoc - the subsequent steps wait for the application to complete (either the application calls Platform.exit()

, or the last window was closed and implicitExit

is true), then JavaFX calls stop()

(which you can override).

+3


source


The start () method of a JavaFX application is called through this call stack:



  • Application # launch: 252
  • LauncherImpl # LaunchApplication: 143
  • LauncherImpl # LaunchApplication: 182
  • LauncherImpl # launchApplication1: 863
  • Application # start (overridden by your application)
+2


source







All Articles