1. Technology

Getting Started With JavaFX Using Netbeans


Hands typing on white keyboard
Blackred/E+/Getty Images

Originally when JavaFX was envisioned to be all about creating rich internet applications writing a JavaFX application would have felt a lot different to writing a Java application. There was a JavaFX scripting language that drove any JavaFX application. However, since JavaFX 2.0, when the functionality of JavaFX became available through the JavaFX API, creating your first JavaFX application will be very familiar. In fact, if you use NetBeans as in this tutorial then creating a JavaFX hello world program couldn't be easier.

Note: If you want to look at an overview of what JavaFX is and how it has progressed through it's various releases then have a look at What Is JavaFX?.

Note: It's best to use the latest version of NetBeans for this tutorial as the NetBeans releases tend to mirror the Java releases these days.The other reason is JavaFX 2.x was not supported by NetBeans until NetBeans v7.1.

Hello World!

Traditionally learning a new programming language has always started with a hello world program - a simple program that says hello to the user.. Java was no different and the guys at NetBeans didn't see any reason for this to change with JavaFX.

To create a JavaFX application in NetBeans go File -> New Project. In the New Project window choose JavaFX from the Categories list. In the Projects list choose JavaFX Application and click Next.On the name and location page chance the application name or location if you want to, otherwise click Finish.

Believe it or not but you have just created a JavaFX Hello World application. That's right, without the need to actually write any JavaFX code! The NetBeans JavaFX application template comes with the code already written in. It does mean that unless you change the NetBeans template you'll be deleting this code every time you create a JavaFX application, but for now it's extremely useful!

The Application Class

What NetBeans has done is create a main class by extending the javafx.application.Application class. This class should be displayed on the right-hand window of the NetBeans project. If you look at the left hand window you'll see the projects pane showing the folders of the JavaFX application.

Inside the application class you will see the following Java code. As you can see it makes use of the JavaFX api:

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 FirstJavaFXApp extends Application {

     * @param args the command line arguments
    public static void main(String[] args) {
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent%gt;() {

            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
        StackPane root = new StackPane();
        primaryStage.setScene(new Scene(root, 300, 250));

If you run the project a window will appear with a Say Hello World button in the center. If you press the button a "Hello World!" will be written to the output window.

Note: If you're new to NetBeans running a program is done by pressing the green 'play' button on the toolbar.

As you can see from the above code there is still a main method. However, the code in the main method simply launches the JavaFX application as it really starts from the start method. This is discussed more in Anatomy of a JavaFX Application.

Even though we talk about creating a JavaFX application it's really just a Java application that's using the JavaFX api. It's just the same as creating a Java application that uses the Swing api.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. JavaFX
  6. Getting Started With JavaFX Using Netbeans

©2014 About.com. All rights reserved.