JavaFX Guide & Java FX tutorial

Introduction to Java FX

JavaFX provides a toolkit for creating cross-platform graphical applications on the Java platform.

JavaFX allows you to create applications with rich, rich graphics using hardware graphics acceleration and GPU capabilities.

With JavaFX, you can create programs for various operating systems: Windows, MacOS, Linux, Android, iOS and for a wide variety of devices: desktops, smartphones, tablets, embedded devices, TV. A JavaFX application will run wherever the Java Executable Environment (JRE) is installed.

JavaFX provides a lot of power compared to a number of other similar platforms, in particular, compared to Swing. This is a large set of controls, and the ability to work with multimedia, two-dimensional and three-dimensional graphics, a declarative way to describe the interface using the FXML markup language, the ability to style the interface using CSS, integration with Swing, and much more.

The history of JavaFX actually began in the first half of the 2000s, when a developer named Chris Oliver, while an employee of SeeBeyond, developed a new language F3 (Froms Follows Functions) for creating graphical interfaces. Subsequently, in 2005, SeeBeyond was acquired by Sun Microsystems (which at that time was developing the Java language before being acquired by Oracle). F3 was renamed to JavaFX, and Chris Oliver continued to work on the new platform already within Sun. And in May 2007, Sun Microsystems publicly announced a new platform for building graphics applications. And on December 4, 2008, the JavaFX 1.0 SDK was released.

Following the acquisition of Sun Microsystems by Oracle in 2010, JavaFX 2.0 was announced and released in 2011. In the first version of JavaFX, it actually represented the Skipto language. In the second version, the approach was completely changed. The scripting language was removed, and the platform was completely rewritten from scratch. Now it was possible to create applications using any language that the JVM supported. New APIs, Swing integration and many other things have been added.

The next important milestones in the development of the platform were JavaFX 8 and especially JavaFX 9, which was released in September 2017 along with Java 9 and brought modularity to the platform. And if earlier JavaFX was supplied with Java SE, now JavaFX is separated from the main functionality of Java SE and is used as a separate module. The latest version of the framework – JavaFX 17 – was released in September 2021.

Currently, JavaFX represents the preferred way to create graphical applications using the Java language, which has come to replace AWT and Swing. It is also worth noting that in order to work with JavaFX, instead of Java, you can theoretically use any programming language that is supported by the JVM.

Installing the JavaFX Toolkit

What is required to work with JavaFX? First of all, you need to install the latest version of the JDK from the official Oracle website: https://www.oracle.com/java/technologies/downloads/.

Keep in mind that the JDK version to work with JavaFX must be 11 or higher.

JavaFX SDK

You also need to download the latest JavaFX SDK from https://gluonhq.com/products/javafx/.

JavaFX SDK - download - ASJAVA

On this page, you can select distributions for various systems and architectures. For example, if the 64-bit OC is Windows, then you need to download the JavaFX Windows x64 SDK accordingly. In essence, the SDK is an archive with files, and after downloading it, you need to unpack it to any preferred location on your hard drive. For example, in my case, the SDK is unpacked to the C:\javafx-sdk-17.0.0.1 folder.

JavaFX Modules

If we open the lib folder in the unpacked SDK, we will see modules there that actually represent JavaFX.

JavaFX 1.6 - ASJAVA

When writing application code, as well as when compiling and running them, we will use these modules. What are they doing:

javafx.base: defines the core functionality of the framework, such as binding functionality, properties, collections, events, etc.

javafx.controls: Defines controls, charts, and skins.

javafx.fxml: defines functionality for working with FXML.

javafx.graphics: Defines the functionality of layout windows and containers, application life cycle, drawing capabilities, user input, animation, css, etc.

javafx.media: defines functionality for working with multimedia.

javafx.swing: Defines an interface for interacting with and embedding Swing elements in a JavaFX application.

javafx.web: defines the functionality of the WebView.

javafx-swt: module for interacting with SWT.

First JavaFX Program

Let’s create the first very simple JavaFX program. First, let’s define a directory on the hard disk to store the files with the source code. Let’s say in my case it will be the C:/javafx directory.

In this directory, create a Main.java file and define the following code in it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.text.Text;
public class Main extends Application{

public static void main(String[] args) {

launch(args);
}

@Override
public void start(Stage stage) {

// inscription installation
Text text = new Text("Hello METANIT.COM!");
text.setLayoutY(80); // setting the position of the inscription along the Y axis
text.setLayoutX(80); // setting the position of the inscription along the X axis

Group group = new Group(text);

Scene scene = new Scene(group);
stage.setScene(scene);
stage.setTitle("JavaFX Application");
stage.setWidth(300);
stage.setHeight(250);
stage.show();
}
}

Let’s take a look at this class briefly. The main JavaFX application class must inherit from the javafx.application.Application class. This class has one abstract method that we need to implement:

public void start(Stage stage) {}

This method is passed as a parameter a Stage object that represents the user interface. For example, on desktops, Stage will represent the graphics window. When the application starts, when the JavaFX framework will call this method and pass the Stage object to it.

What do we need to create a GUI? First of all, we need various visual elements, such as buttons, text fields, lists, images, and so on. JavaFX provides a large set of built-in visuals. In this case, we are using the javafx.scene.text.Text visual component, which represents a simple text label. When creating a Text element, we can pass the text to be displayed to it, as well as adjust its position using its methods:

Text text = new Text("Hello METANIT.COM!");     // creating an inscription
text.setLayoutY(80);        // setting the position of the inscription along the Y axis
text.setLayoutX(80);    // setting the position of the inscription along the X axis

All visual elements that we want to display in the Stage are placed in the javafx.scene.Scene object or on the stage. Scene is the top level container for all graphic elements. However, we cannot place the Text object directly in the Scene. The Scene class provides for setting the root element or container that contains all other elements.

Thus, the Text element is first placed inside a Group element, which represents a container for a group of elements. The Group element is then set as the root element of the Scene. And at the end, the Scene element is set to the Stage object.

Group group = new Group(text);  // creating and setting a group of elements
Scene scene = new Scene(group);     // scene creation
stage.setScene(scene);                  // setting the stage for the Stage object
At the end of the start method, we can set up the Stage object, for example, set the title of the window, as well as its dimensions:
stage.setTitle("JavaFX Application"); // setting the window title
stage.setWidth(300);        // setting window width
stage.setHeight(250);       // window length setting
stage.show();               // display a window on the device screen

With the show method, the Stage object is displayed on the screen of the device.

But the Main class, like any main Java application class, starts with the main method. And in the method, to launch the JavaFX application itself, represented by the Application class, the launch() method is called:

public static void main(String[] args) {
launch(args);
}

The launch method actually launches the JavaFX application. After that, the start method is called and the application window will be displayed on the screen.

Now let’s compile and run the application. In the last topic, we looked at the JavaFX SDK and modules. In my case, the JavaFX SDK was unpacked to the C:\javafx-sdk-17.0.0.1 folder, so the modules are located in the C:\javafx-sdk-17.0.0.1\lib folder. Next, we will use this location to compile and run the application.

First, using the cd command, go to the command line/terminal to the directory where the source code file is located. Then we compile the application using the command

javac --module-path C:\javafx-sdk-17.0.0.1\lib --add-modules=javafx.controls Main.java

The standard Java compiler, javac, is used for compilation. It is passed the –module-path parameter, which specifies the location of the modules. In each case, depending on where the SDK is located, this path may differ. In addition, the add-modules parameter is specified, which specifies the modules to use. In this case, the javafx.controls module is used, which contains references to other modules.

Next, run the compiled application using the command

java –module-path C:\javafx-sdk-17.0.0.1\lib –add-modules=javafx.controls Main

1.1 JavaFX

As a result, we will see the following window:

JavaFX application 1.2


Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *