JavaFX Tutorial - JavaFX HelloWorld








The following code shows how to create a window in JavaFX and add a button control to it. It also shows how to handle click event for the button.

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;
//w w  w.j  ava 2s  .  c  o m
public class Main 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);
  }
}

The code above generates the following result.

null




Note

The Main class for a JavaFX application extends the javafx.application.Application class. The start() method is the main entry point for all JavaFX applications.

javafx.application.Application class provides application life cycle functions such as initializing, launching, starting, and stopping during runtime.

Application class provides a way for Java applications to launch JavaFX GUI components separate from the main thread.

main() method launches the JavaFX application by passing in the command-line arguments to the Application.launch() method.

To access any arguments passed into the launch() method we can invoke the getParameters() method of the Application class.

After executing the Application.launch() method, the JavaFX application will enter a ready state, and the framework internals call the start() method to start.

After invoking the start() method, the program runs in the JavaFX thread and not on the main thread and a JavaFX javafx.stage.Stage object is available for the developer to use.

The following code shows the overridden Application start() method:

@Override
public void  start(Stage primaryStage) {...}




JavaFX Scene Graph

A JavaFX application defines the user interface container by a stage and a scene.

The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content.

Stage and Scene are oddly named compared to the Swing name, such as JFrame, JPanel.

The JavaFX API models things as a theater or a play.

In the theater actors perform a play in front of an audience. A play consists of one-to-many scenes. All scenes are performed on a stage.

In JavaFX the Stage is equivalent to an application window similar to Java Swing API JFrame or JDialog.

The Scene object is a content pane, similar to Java Swing's JPanel. Each Scene can hold children Node objects.

JavaFX Node

In JavaFX, the scene content is represented as a hierarchical scene graph of nodes.

A JavaFX Node is a fundamental base class for all scene graph nodes.

A Node can scale, transform, translation, and has effects.

The most commonly used nodes are UI controls and Shape objects.

A scene graph can contain children nodes by using a container class such as the Group or Pane class.

In the code above, the root node is a StackPane object, which is a resizable layout node.

The root node contains one child node, a Button.

The button has an event handler to print a message when the button is pressed.

Once the child nodes have been added to our root Group via the getChildren().add() method, we set the primaryStage's scene and call the show() method on the Stage object to display a JavaFX application window.

By default the window will allow a user to minimize, maximize, and exit the application.

Following is the code to set the scene and show the JavaFX application window (Stage):

primaryStage.setScene(scene);
primaryStage.show();

The main() method is not required for JavaFX applications when the JAR file is created using JavaFX Packager tool.

JavaFX Packager tool would embed the JavaFX Launcher in the JAR file.

Example

The following code shows how to process commandline parameters.

import java.util.List;
//from   w  w w  .j  a  v  a 2 s  . c o m
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        
        final Parameters params = getParameters();
        final List<String> parameters = params.getRaw();
        final String imageUrl = !parameters.isEmpty() ? parameters.get(0) : "";
        
        primaryStage.show();
    }
}