HelloFXApp.java

Sevil Topal
3 min readFeb 13, 2024

Today, I’m sharing a different article than I used to share with you. As you may guess from the title it is about Java FX for starters :) As I have an exam on Java, I was programming last days on Java by using FX and would like to share with you too.

JavaFX is an open source Java-based GUI framework that is used to develop rich client applications. It is the successor of Swing in the arena of GUI development technology on the Java platform.

The GUI in JavaFX is shown in a stage. A stage is an instance of the Stage class. A stage is a window in a desktop application. A stage contains a scene. A scene contains a group of nodes (graphics) arranged in a tree-like structure.

A JavaFX application inherits from the Application class. The JavaFX runtime creates the first stage called the primary stage and calls the start() method of the application class passing the reference of the primary stage. The developer needs to add a scene to the stage and make the stage visible inside the start() method.

I’m using IntelliJ as IDE. After adding the JavaFX modules to your IDE, you’re ready to go.

Let’s write your first JavaFX application. It should display the text “Hello JavaFX” in a window.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;



public class HelloFXApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
Text msg = new Text("Hello JavaFX");
VBox root = new VBox();
root.getChildren().add(msg);
Scene scene = new Scene(root, 300, 50);
stage.setScene(scene);
stage.setTitle(
"Hello JavaFX Application with a Scene");
stage.show();
}
}

Note: Every JavaFX application class must inherit from the Application class and provide the implementation for the start(Stage stage) method.

Note2: Any node that inherits from the javafx.scene. Parent class can be used as the root node for a scene. Several nodes, known as layout panes or containers such as VBox, HBox, Pane, FlowPane, GridPane, or TilePane, can be used as a root node. Group is a special container that groups its children together.

JavaFX is capable of doing much more than you have seen so far. Let’s enhance the first program and add some more user interface elements such as buttons and text fields. This time, the user will be able to interact with the application.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ImprovedHelloFXApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
Label nameLbl = new Label("Enter your name:");
TextField nameFld = new TextField();
Label msg = new Label();
msg.setStyle("-fx-text-fill: blue;");
// Create buttons
Button sayHelloBtn = new Button("Say Hello");
Button exitBtn = new Button("Exit");
// Add the event handler for the Say Hello button
sayHelloBtn.setOnAction(e -> {
String name = nameFld.getText();
if (name.trim().length() > 0) {
msg.setText("Hello " + name);
} else {
msg.setText("Hello there");
}


});
// Add the event handler for the Exit button
exitBtn.setOnAction(e -> Platform.exit());
// Create the root node
VBox root = new VBox();
// Set the vertical spacing between children to 5px
root.setSpacing(5);
// Add children to the root node
root.getChildren().addAll(nameLbl, nameFld, msg,
sayHelloBtn, exitBtn);
Scene scene = new Scene(root, 350, 150);
stage.setScene(scene);
stage.setTitle("Improved Hello JavaFX Application");
stage.show();
}
}

The improved HelloFX program displays a window. The window contains two labels, a text field, and two buttons. A VBox is used as the root node for the scene. Enter a name in the text field and click the Say Hello button to see a hello message. Clicking the Say Hello button without entering a name displays the message Hello there. The application displays a message in a Label control. Click the Exit button to exit the application.

You can launch a JavaFX application using the launch() method of the Application class.

During the lifetime of a JavaFX application, the JavaFX runtime calls predefined methods of the JavaFX Application class in a specific order. First, the no-args constructor of the class is called, followed by calls to the init() and start() methods. When the application terminates, the stop() method is called. You can terminate a JavaFX application by calling the Platform.exit() method.

Hope you learned something new and enjoyed the reading!

Resource: Learn JavaFX 17: Building User Experience and Interfaces with Java

--

--

Sevil Topal

MSc @ TUM, Agile Coach @ MMS, SM, Industrial Engineer, Wanderluster, texting about business, agility, scrum, wellness, productivity, travel, and 20’s life.