Basic Code for JavaFX Applications

The basic code for JavaFX is quite different from AWT and Swing. Let’s first look at the basics: which packages and classes are used to create an application.

What Is JavaFX?

Some time has passed since the long-awaited Java 8 appeared. Have you become used to this new version? Java 8 added many new features, but one of the biggest changes for Java is the shift from Swing to JavaFX.

Until now, the GUI libraries used to develop desktop applications in Java were AWT and Swing. Even so, AWT mainly provided the base GUI parts, and in practice Swing, which was built on top of it, was what people used. Swing has long been widely used as the standard Java GUI.

With Java 8, however, the new JavaFX was included as standard, and the shift from Swing to JavaFX was decided. Of course, Swing will coexist for the time being, so it will not disappear immediately, but it seems almost certain that Swing will eventually stop and JavaFX will become the single standard. This is stated on Oracle’s website, which currently handles Java development. http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html#6 “Is JavaFX replacing Swing as the new client UI library for Java SE?”

So what exactly is JavaFX?

Many people who have used Swing for a long time may think, “Isn’t it just an improved version based on AWT and Swing? If I look at it briefly, I should be able to use it right away.” When Swing appeared, its contents were very new, but the basic structure, such as the event system and graphics rendering system, inherited AWT as-is, so there was not that much confusion. Of course, many new GUI components were added, so it was quite different from AWT, but there was still no need to understand everything again from the fundamental concepts. Swing was not a “reform” or “revolution.”

JavaFX is different. It is a revolution. The existing AWT/Swing concepts almost do not apply. Old class names such as Button and TextField are inherited as-is, but Stage, which integrates them, is a class you have probably never seen before. In fact, Button and TextField can also be written in XML code. First of all, you do not inherit from a Frame class because there is no such class. You also do not use event processing through event listeners, at least not in the old sense. There is no paint method, no Graphics drawing method, and no corresponding class.

JavaFX is a completely different system from the basic GUI system of AWT/Swing. Keep that point clearly in mind.

Basic Code for the Application Class

Now let’s look at the basics of how to create a JavaFX application. I may have made it sound intimidating by saying that “the GUI foundation is different,” and some readers may be wondering how to write programs. But there is no need to worry that much.

The basic structure of an application is still the same as ordinary Java: create a class that implements the main method and run it. In other words, you can create a JavaFX application by writing a normal Java class. There is no change on this point.

However, a JavaFX application class must be created by extending an unfamiliar class named Application.

Remember the most basic form of an application as shown below. This is the basis of a JavaFX application.

public class ClassName extends Application {
 
    public static void main(String[] args) {...}
 
    @Override
    public void start(Stage stage) throws Exception {...}
 
}

The JavaFX library is contained in a package named javafx. An application extends a class named Application in the javafx.application package. This class is an abstract class and includes a method named start, which must be implemented.

The start method is where you write the processing that runs when the application starts. An instance of the Stage class from the javafx.stage package is passed to this method as an argument. A Stage is a “top-level container,” the container that becomes the foundation of the GUI. In AWT/Swing, windows were created and displayed with classes such as Frame and JFrame, but in JavaFX it is basic to build windows using this Stage.

Unlike Swing and similar libraries, JavaFX prepares the window body that integrates the GUI on the JavaFX side. You do not prepare a class that extends Stage and create an instance of it. When the application starts, a Stage instance is passed to start as the first application window to be displayed. The programmer simply uses that Stage to build the GUI.

Create an Application

Now let’s create and run a simple application. Write the following sample code.

package com.devkuma.javafx;
 
import javafx.application.Application;
import javafx.stage.Stage;
 
public class App extends Application {
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) throws Exception {
        stage.show();
    }
 
}

Here, a class named App is created in the com.devkuma.javafx package. Use whatever package and class names are appropriate.

When you run this, a blank white window appears on the screen. If you click the close button, the application ends. It is extremely simple. Let’s summarize what is happening here.

The launch Method

First, the main method calls the launch method included in the Application class. It passes the String array received by the class’s main method directly as an argument and starts the JavaFX application. Remember that a JavaFX application starts with launch.

The show Method

In the start method, the show method of the Stage instance passed as an argument is called. This displays the window built in that Stage on the screen. There is also a hide method that hides the window.

Programmers who have used Swing may feel uncomfortable with using show/hide to display a window, because until now they probably used setVisible instead of show/hide. However, remember that Stage does not have setVisible.

Scene, Pane, and Components

Now add a simple component to this window. As the simplest example, add an element that only displays text.

The source code below is an example.

package com.devkuma.javafx;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
 
public class App extends Application {
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) throws Exception {
        Label label = new Label("This is JavaFX!");
        BorderPane pane = new BorderPane();
        pane.setCenter(label);
        Scene scene = new Scene(pane, 320, 240);
        stage.setScene(scene);
        stage.show();
    }
 
}

When you run this, the text This is JavaFX! is displayed in the center of the window.

For displaying text, JavaFX includes a class named Label, just as AWT/Swing did. However, this is different from the AWT/Swing Label. It is JavaFX-specific and is included in the javafx.scene.control package.

With this, text can be displayed. But can you simply add a Label directly to a Stage? No. To use a Stage, you must use Scene and Pane.

What Is a Scene?

A Scene is an invisible container included in a Stage. A Stage is the window, but the Scene builds the contents displayed in that window. What is contained in the Scene is placed into the Stage and displayed. A Scene instance is created in the following form.

new Scene(Pane, width, height);

As you can tell, width and height are the size of the container to create. When this is placed into the Stage, the window is adjusted to that size.

So what is a Pane? It is the base container that holds component types. A Scene can contain only one Pane.

There is not just one kind of Pane. There are several types. The layout method changes depending on what kind of components the Pane contains. In AWT/Swing terms, it is like a layout manager. However, unlike a layout manager that only handles layout management, you can think of a Pane as “a container with layout manager functionality built in.”

To summarize, the structure of a window that uses components is as follows.

  • A Stage contains a Scene.
  • A Scene contains a Pane.
  • A Pane contains components.

Only then can component types finally be used. The structure is somewhat different, but because Swing and similar systems also created screens by combining containers, it should not feel too strange.

BorderPane

I said that there are several kinds of Pane. In the previous example, we used a BorderPane. You can think of it as a container with BorderLayout built in.

This BorderPane creates an instance without arguments, as in new BorderPane(). Components are added by calling the basic methods.

Place in the center

*BorderPane*.setCenter(component);

Place at the top

*BorderPane*.setTop(component);

Place at the bottom

*BorderPane*.setBottom(component);

Place on the right

*BorderPane*.setRight(component);

Place on the left

*BorderPane*.setLeft(component);

With these methods, you can position five components: top, bottom, left, right, and center. In any case, it feels like BorderLayout, so it should be easy to understand intuitively.

FlowPane

Several other Pane classes are also available. One of them is FlowPane. It is similar to FlowLayout and displays added components in order.

new FlowPane();

Create an instance without arguments like this. The layout structure is the same as FlowLayout, but the management of contained components is a little different.

Inside a FlowPane, the contained component types are managed together in a List. This List instance can be obtained with the getChildren method. Add components to this List so they are displayed. For example:

pane.getChildren().add(label);

You can see that components are added in this way. Many people may find this somewhat inconvenient, but do not think that “managing with a List” is just a special method for handling components inside a container. It is different from an ordinary collection.

For now, if you learn BorderPane and FlowPane, you will be able to use the basic built-in components. Other Pane classes will be covered again later.

package com.devkuma.javafx;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
 
public class App extends Application {
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) throws Exception {
        Label label = new Label("This is JavaFX!");
        FlowPane pane = new FlowPane();
        pane.getChildren().add(label);
        Scene scene = new Scene(pane, 320, 240);
        stage.setScene(scene);
        stage.show();
    }
 
}