Your First JavaFX Application creation

JavaFX Application creation manualLet’s write your first JavaFX application. It should display the text “Hello JavaFX” in a window. I will take an incremental, step-by-step approach to explain how to develop this first application. I will add as few lines of code as possible, and then, explain what the code does and why it is needed.

 

Creating the HelloJavaFX Class

A JavaFX application is a class that must inherit from the Application class that is in the javafx.application package. You will name your class HelloFXApp and it will be stored in the com.jdojo.intro package. Listing 1 shows the initial code for the HelloFXApp class. Note that the HelloFXApp class will not compile at this point. You will fix it in the next section.


Table of contents[Show]


Listing 1. Inheriting Your JavaFX Application Class from the javafx.application.Application Class

// HelloFXApp.java

package com.jdojo.intro;

import javafx.application.Application;

public class HelloFXApp extends Application {

       // Application logic goes here

}

The program includes a package declaration, an import statement, and a class declaration. There is nothing like JavaFX in the code. It looks like any other Java program. However, you have fulfilled one of the requirements of the JavaFX application by inheriting the HelloFXApp class from the Application class.

 

Overriding the start() Method

If you try compiling the HelloFXApp class, it will result in the following compile-time error: HelloFXApp is not abstract and does not override abstract method start(Stage) in Application. The error is stating that the Application class contains an abstract start(Stage stage) method, which has not been overridden in the HelloFXApp class. As a Java developer, you know what to do next: you either declare the HelloFXApp class as abstract or provide an implementation for the start() method. Here let’s provide an implementation for the start() method. The start() method in the Application class is declared as follows:

public abstract void start(Stage stage) throws java.lang.Exception

Listing 2 shows the revised code for the HelloFXApp class that overrides the start() method.

Listing 2. Overriding the start() Method in Your JavaFX Application Class

// HelloFXApp.java


package com.jdojo.intro;

import javafx.application.Application;
import javafx.stage.Stage;


public class HelloFXApp extends Application {

       @Override

       public void start(Stage stage) {

               // The logic for starting the application goes here

       }

}

In the revised code, you have incorporated two things:

  • You have added one more import statement to import the Stage class from the javafx.stage package.
  • You have implemented the start() method. The throws clause for the method is dropped, which is fine by the rules for overriding methods in Java.

The start() method is the entry point for a JavaFX application. It is called by the JavaFX application launcher. Notice that the start() method is passed an instance of the Stage class, which is known as the primary stage of the application. You can create more stages as necessary in your application. However, the primary stage is always created by the JavaFX runtime for you.

Tip

 

Showing the Stage

Similar to a stage in the real world, a JavaFX stage is used to display a scene. A scene has visuals—such as text, shapes, images, controls, animations, and effects—with which the user may interact, as is the case with all GUI-based applications.

In JavaFX, the primary stage is a container for a scene. The stage look-and-feel is different depending on the environment your application is run in. You do not need to take any action based on the environment because the JavaFX runtime takes care of all the details for you. For example, if the application runs as a desktop application, the primary stage will be a window with a title bar and an area to display the scene; if the application runs an applet in a web browser, the primary stage will be an embedded area in the browser window.

The primary stage created by the application launcher does not have a scene. You will create a scene for your stage in the next section.

You must show the stage to see the visuals contained in its scene. Use the show() method to show the stage. Optionally, you can set a title for the stage using the setTitle() method. The revised code for the HelloFXApp class is shown in Listing 3.

Listing 3. Showing the Primary Stage in Your JavaFX Application Class

// HelloFXApp.java


package com.jdojo.intro;

import javafx.application.Application;
import javafx.stage.Stage;


public class HelloFXApp extends Application {


       @Override

       public void start(Stage stage) {


               // Set a title for the stage

               stage.setTitle("Hello JavaFX Application");

               // Show the stage

               stage.show();

       }

}

 

Launching the Application

You are now ready to run your first JavaFX application. You can use one of the following two options to run it:

  • It is not necessary to have a main() method in the class to start a JavaFX application. When you run a Java class that inherits from the Application class, the java command launches the JavaFX application if the class being run does not contain the main() method.
  • If you include a main() method in the JavaFX application class inside the main() method, call the launch() static method of the Application class to launch the application. The launch() method takes a String array as an argument, which are the parameters passed to the JavaFX application.

If you are using the first option, you do not need to write any additional code for the HelloFXApp class. If you are using the second option, the revised code for the HelloFXApp class with the main() method will be as shown in Listing 1-4.

Listing 4. The HelloFXApp JavaFX Application Without a Scene

// HelloFXApp.java

package com.jdojo.intro;

import javafx.application.Application;
import javafx.stage.Stage;


public class HelloFXApp extends Application {

       public static void main(String[] args) {

               // Launch the JavaFX application
               Application.launch(args);

       }


       @Override

       public void start(Stage stage) {

               stage.setTitle("Hello JavaFX Application");

               stage.show();


       }
}

The main() method calls the launch() method, which will do some setup work and call the start() method of the HelloFXApp class. Your start() method sets the title for the primary stage and shows the stage. Compile the HelloFXApp class using the following command:

javac com/jdojo/intro/HelloFXApp.java

Run the HelloFXApp class using the following command, which will display a window with a title bar as shown in Figure 1:

java com.jdojo.intro.HelloFXApp

 The HelloFXApp JavaFX Application Without a Scene

Figure 1. The HelloFXApp JavaFX Application Without a Scene

The main area of the window is empty. This is the content area in which the stage will show its scene. Because you do not have a scene for your stage yet, you will see an empty area. The title bar shows the title that you have set in the start() method.

You can close the application using the Close menu option in the window title bar. Use Alt + F4 to close the window in Windows. You can use any other option to close the window as provided by your platform.

Tip

You haven’t seen anything exciting in JavaFX yet! You need to wait for that until you create a scene in the next section.

 

Adding the main() Method

As described in the previous section, the Java 8 launcher (the java command) does not require a main() method to launch a JavaFX application. If the class that you want to run inherits from the Application class, the java command launches the JavaFX application by automatically calling the Application.launch() method for you.

If you are using the NetBeans IDE to create the JavaFX project, you do not need to have a main() method to launch your JavaFX application if you run the application by running the JavaFX project. However, the NetBeans IDE requires you to have a main() method when you run the JavaFX application class as a file, for example, by selecting the HelloFXApp file, right-clicking it, and selecting the Run File option from the menu.

Some IDEs still require the main() method to launch a JavaFX application. All examples in this blog will include the main() method that will launch the JavaFX applications.

 

Adding a Scene to the Stage

An instance of the Scene class, which is in the javafx.scene package, represents a scene. A stage contains one scene, and a scene contains visual contents.

The contents of the scene are arranged in a tree-like hierarchy. At the top of the hierarchy is the root node. The root node may contain child nodes, which in turn may contain their child nodes, and so on. You must have a root node to create a scene. You will use a VBox as the root node. VBox stands for vertical box, which arranges its children vertically in a column. The following statement creates a VBox:

VBox root = new VBox();

Tip

A node that can have children provides a getChildren() method that returns an ObservableList of its children. To add a child node to a node, simply add the child node to the ObservableList. The following snippet of code adds a Text node to a VBox:

// Create a VBox node

VBox root = new VBox();


// Create a Text node


Text msg = new Text("Hello JavaFX");


// Add the Text node to the VBox as a child node

root.getChildren().add(msg);

The Scene class contains several constructors. You will use the one that lets you specify the root node and the size of the scene. The following statement creates a scene with the VBox as the root node, with 300px width and 50px height:

 

// Create a scene

Scene scene = new Scene(root, 300, 50);

You need to set the scene to the stage by calling the setScene() method of the Stage class:

// Set the scene to the stage

stage.setScene(scene);

That’s it. You have completed your first JavaFX program with a scene. Listing 5 contains the complete program. The program displays a window as shown in Figure 1-3.

Listing 5. A JavaFX Application with a Scene Having a Text Node

// HelloFXAppWithAScene.java

package com.jdojo.intro;

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


public class HelloFXAppWithAScene 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();



       }



}

 JavaFX application with a scene having a Text node

Figure 2. A JavaFX application with a scene having a Text node

Вас заинтересует / Intresting for you:

Getting Started with Java prog...
Getting Started with Java prog... 402 views Doctor Thu, 02 Aug 2018, 04:05:33
What Is JavaFX?
What Is JavaFX? 652 views Taniani Wed, 04 Jul 2018, 04:34:21
Creating TinyCalculator web ap...
Creating TinyCalculator web ap... 1084 views Zero Cool Sun, 23 Sep 2018, 11:10:19
J2ME Specifications and other ...
J2ME Specifications and other ... 343 views Максим Николенко Sun, 10 Jun 2018, 16:30:17
Taniani
Author: Taniani
More by this author:

Comments on Your First JavaFX Application creation

Be the first to comment
Please login to comment