The entry point for Java programs we have written so far this year is the main() method.
public class Driver {
public static void main(String[] args) {
// Entry point
}
}
In a JavaFX program, the entry point is the start() method of a class that extends javafx.application.Application
. Here is the Application API for reference
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApp extends Application {
@Override
public void start(Stage stage) throws Exception {
// Entry point
}
}
Note in the API that Application is abstract because it contains an abstract method named start(). This is the entry point for a JavaFX application and your class must override this abstract method.
Finally, to run a JavaFX program you need to call a JavaFX method named launch() in your program's main() method.
launch() is a special method of the Application class that sets up a JavaFX application and calls start() for you. Never call start() directly. Read the top section of the Application API if you want to know why.
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApp extends Application {
// You still need a main method, but it calls launch to start JavaFX
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// Entry point
stage.setTitle("My Window (aka Stage)");
stage.setWidth(500);
stage.setHeight(500);
stage.show();
}
}
Last modified: February 27, 2023
Back to Test Your Javafx InstallationDark Mode
Outline