3 Hello World, JavaFX Style (Release 8) (original) (raw)

The best way to teach you what it is like to create and build a JavaFX application is with a ”Hello World” application. An added benefit of this tutorial is that it enables you to test that your JavaFX technology is properly installed.

The tool used in this tutorial is NetBeans IDE 7.4. Before you begin, ensure that the version of NetBeans IDE that you are using supports JavaFX 8. See the Certified System Configurations section of the Java SE 8 downloads page for details.

Construct the Application

  1. From the File menu, choose New Project.
  2. In the JavaFX application category, choose JavaFX Application. Click Next.
  3. Name the project HelloWorld and click Finish.
    NetBeans opens the HelloWorld.java file and populates it with the code for a basic Hello World application, as shown in Example 3-1.
    Example 3-1 Hello World
    package helloworld;

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;

public class HelloWorld extends Application {

@Override  
public void start(Stage primaryStage) {  
    Button btn = new Button();  
    btn.setText("Say 'Hello World'");  
    btn.setOnAction(new EventHandler<ActionEvent>() {  

        @Override  
        public void handle(ActionEvent event) {  
            System.out.println("Hello World!");  
        }  
    });  
      
    StackPane root = new StackPane();  
    root.getChildren().add(btn);  

Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Here are the important things to know about the basic structure of a JavaFX application:

Figure 3-1 shows the scene graph for the Hello World application. For more information on scene graphs see Working with the JavaFX Scene Graph.

Run the Application

  1. In the Projects window, right-click the HelloWorld project node and choose Run.
  2. Click the Say Hello World button.
  3. Verify that the text ”Hello World!” is printed to the NetBeans output window.
    Figure 3-2 shows the Hello World application, JavaFX style.

Where to Go Next

This concludes the basic Hello World tutorial, but continue reading for more lessons on developing JavaFX applications:

Close Window

Table of Contents

JavaFX: Getting Started with JavaFX

Expand | Collapse