Hello world program in JavaFX


Hello world program in JavaFX :

There is three way to make a hello world program

  1. Simple code using classes
  2. Using FXML (XML)
  3. Using Scene builder

To write a hello world you should understand following terminology :

  • stage : A main empty window
  • Scene : It can be considered as a content pane capable of holding UI controls and Shape objects.
  • Rootpane : it is a container in which we can put buttons, text fields, menu etc.
  • Example – stackpane, borderpane etc.

javafx stage,scene, beginners tutorial

Note –>

    Here we will teach you all the program using netbeans IDE with code.

Steps to create a hello world program using netbeans

    Go to the top left corner and select “File”
    And then “new project”

javafx netbeans, javafx tutorials

    A new popup windows As

javafx tutorial, javafx project,javafx netbeans

    Select “javafx” in categories and “javafx application” in project section As shown in figure above.
    Click on “next” .
    A new window open

netbeans javafx tutorial, netbeans javafx

    Fill the “project name” then click “finish”
    A new full window is open with “hello world” program.

Life Cycle of Javafx application :

javafx life cycle ,javafx tutorials

  • Entry point for JavaFX applications is the Application class.
  • constructs an instance of the specified Application class
    Calls the init() method
  • Calls the start(javafx.stage.Stage) method
  • Waits for the application to finish, which happens when either of the following occur:
    • The application calls Platform.exit()
    • The last window has been closed and the implicit Exit attribute on Platform is true
    • Calls the stop() method
  • Note that the start method is abstract and must be overridden.

Code of “hello world” program in javafx

package javafxtuts;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

 * @author JavaFXtuts.com
public class Javafxtuts extends Application {
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler() {
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");

     * @param args the command line arguments
    public static void main(String[] args) {

Output :

javafx hello world program, javafx netbeans

Explaination of javafx hello world program :

First we extends the Application as you it is the entry point of any javafx application.

    –> “extends Application”

We have to extend it for working javafx application. It is an abstract class (which means, that you cannot instantiate it) with a single abstract method start, that you have to override.

Now in the main method there as a method “launch()” called with parameter. But here in this example there is no parameter so it is like “launch(null)”.

In the program skip the “setOnAction()” method for later.

Then control goes to “Application” class and it work as shown above.

Then “start ()” method called and inside it all the logic specified.

Here rootpane is “Stackpane”
“Scene” take rootpane as :

Scene scene= new Scene(root, width,height);

And we set scene to stage using “setScene()” AS


At last we call show method to show the application AS


We can use the setTitle() method to set the title of the Stages.


Leave A Reply