Green Bean Java Drinking Machine all about Java FX

13.1 JavaFX | Introducing JavaFX

So in this tutorial I will be introducing the frameworks that are responsible for developing Graphical User Interfaces (GUI) through Java. That is JavaFX.

Note that possibly the first major implementation of GUI in Java was through GUI classes bundled into a library called Abstract Windows Toolkit (AWT). This was then replaced by the Swing Components library. Java Swing focuses on building desktop GUI applications. Finally, Java Swing got replaced with JavaFX. JavaFX allows you to make GUIs for Rich Internet Applications (RIA). RIA is a web application that delivers similar features and functions of a regular desktop application. So  AWT >> Swing >> JavaFX.

Table of Contents

A Simple JavaFX Program

The main framework for writing JavaFX programs is defined by the javafx.application.Application abstract class.

Here’s a simple JavaFX program that makes OK button. I will explain along the way, with comments in the code and commentary after:

import javafx.application.Application; // main framework
import javafx.scene.Scene; // The JavaFX Scene class is the container for all content in a scene graph.
import javafx.scene.control.Button; // Allows you to make simple buttons
import javafx.stage.Stage; // ...Top level JavaFX container

public class MyJavaFX extends Application { // MyJavaFX is copying the fields and methods of the Application class
    @Override // Override the start method in the Application class; @Override is called an annotation.
    public void start(Stage primaryStage) {
        // Make a scene and place a button in the scene
        Button btOK = new Button("OK"); // Make a button
        Scene scene = new Scene(btOK, 200, 250); // Make a scene
        primaryStage.setTitle("MyJavaFX"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Displays the stage
    }

    /**
     * Know that the main method is only needed for an IDE that has limited JavaFX support.
     * There is no need for main method when running JavaFX from the command line.
     */

    public static void main(String[] args) { //The Main method for running the class from an IDE
        Application.launch(args);
    }
}

Output:

MyJavaFX SimpleButtonGUI

Extra Notes

Scene class: Remember I mentioned that javafx.scene.Scene is the class that is the “container” for all the content in a scene graph? Well, a scene graph is a hierarchical tree of nodes that represent all of the visual elements of the application’s user interface [Source]. A node is each item in a scene graph.

extends: To understand the keyword extends, you first must understand the concept of inheritance. Inheritance is about being able to derive classes from other classes, in other words the inheriter classes are inheriting the fields and methods of the inherit-ee class. Again in other words, a class is copying for itself the fields and methods of another class. This is inheritance. In order to copy the fields and methods of a class, the syntax goes CopyOfClass extends Class. In the example above, the local class MyJavaFX is copying the fields and methods of then Application class.

@Override: @Override is an annotation that acts as a “hint for the compiler to let it know that you’re overriding the method of a parent class” [Source].

Use @Override every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that your method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.[Source]

launch: The launch method (line 23) is a static method defined in the Application class for launching a stand-alone JavaFX application.

main method: The main method (line 22-24) is not needed if you run your JavaFX program from a command line, but needed for IDEs with limited JavaFX support. Know that when you run a JavaFX application without the main method, the Java Virtual Machine (JVM) automatically invokes the launch method that is needed to run JavaFX applications.

main class: Know that the main MyJavaFX class overrides the start method that is defined in the javafx.application.Application class (line 8).

Button: At line 10, a Button object is made and is placed in the Scene object (line 11). Scene Object: A Scene object is made using the constructor Scene(node, width, height). This constructor specifies the scene width, the scene height, and the placement of the nodes in the scene.

Stage: A Stage object is a window. Know that the JVM automatically makes a Stage object called primary stage when the JavaFX application launches.

GUI Composition

MyJavaFX SimpleButtonGUI_LayoutExplained

A Multi-Stage JavaFX Program

Here’s is a JavaFX program that is similar to the one above, but makes an additional pop-up dialog box:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class MultiStageOperations extends Application {
    @Override // Override the start method in the  Application class
    public void start(Stage primaryStage) { // Watch out misspelling start as starts
        // Make a scene and place a button in the scene
        Scene scene = new Scene(new Button("OK"), 200, 250);
        primaryStage.setTitle("MyJavaFX"); // Set the stage title
        primaryStage.setScene(scene); //Place the scene in the stage
        primaryStage.show(); // Display the stage

        Stage stage = new Stage(); // Make a new stage
        stage.setTitle("Secondary Stage"); // Set the stage title
        stage.setScene(new Scene(new Button("New Stage"),250, 230));
        stage.show(); // Display the stage
    }
}

Output:

Results for Multiple Stage Code

Notice there is no main method listed in the code; this is fine as long as your IDE supports JavaFX.

Extra Note:

If you want to prevent a user from resizing the dialog box / stage, add to your stage.setResizable(false) code.


◄◄◄BACK | NEXT►►►

What's Your Opinion?