1. JavaFX - Architecture
JavaFX is a software platform that allows developers to build various
content-rich client applications, which operate consistently across several
platforms.
It is a complete API with a rich set of classes and interfaces to build GUI
applications with rich graphics. Some of the important packages of this
API are –
javafx.animation − Contains classes to add transition based animations such
as fill, fade, rotate, scale and translation, to the JavaFX nodes.
javafx.application − Contains a set of classes responsible for the JavaFX
application life cycle.
javafx.css − Contains classes to add CSSlike styling to JavaFX GUI
applications.
javafx.event − Contains classes and interfaces to deliver and handle JavaFX
events.
javafx.geometry − Contains classes to define 2D objects and perform
operations on them.
javafx.stage − This package holds the top level container classes for JavaFX
application.
javafx.scene − This package provides classes and interfaces to support the
scene graph. In addition, it also provides sub-packages such as canvas,
chart, control, effect, image, input, layout, media, paint, shape, text,
transform, web, etc. There are several components that support this rich
API of JavaFX.
JavaFX Architecture
The following illustration shows the architecture of JavaFX API. Here you
can see the components that support JavaFX API.
2. Scene Graph
In JavaFX, the GUI Applications were coded using a Scene Graph. A Scene Graph is the
starting point of the construction of the GUI Application. It holds the (GUI) application
primitives that are termed as nodes.
A node is a visual/graphical object and it may include −
Geometrical (Graphical) objects − (2D and 3D) such as circle, rectangle,
polygon, etc.
UI controls − such as Button, Checkbox, Choice box, Text Area, etc.
Containers − (layout panes) such as Border Pane, Grid Pane, Flow Pane, etc.
Media elements − such as audio, video and image objects.
In general, a collection of nodes makes a scene graph. All these nodes are
arranged in a hierarchical order as shown below.
Each node in the scene graph has a single parent, and the node which
does not contain any parents is known as the root node.
In the same way, every node has one or more children, and the node
without children is termed as leaf node; a node with children is termed as
a branch node.
A node instance can be added to a scene graph only once. The nodes of a
scene graph can have Effects, Opacity, Transforms, Event Handlers, Event
Handlers, Application Specific States.
3. JavaFX - Application
As we have already learned, JavaFX is an open source free software
platform, that allows a user to develop client applications that work
consistently across various devices. Using JavaFX, one can create Graphical
User Interface applications (GUIs), and Internet or Desktop applications as
well. All these applications will be developed in Java.
JavaFX Application Structure
In general, a JavaFX application will have three major components
namely Stage, Scene and Nodes as shown in the following diagram.
Stage
A stage (a window) contains all the objects
of a JavaFX application. It is represented
by Stage class of the package javafx.stage. The
primary stage is created by the platform
itself. The created stage object is passed as
an argument to the start() method of
the Application class (explained in the next
section).
A stage has two parameters determining its
position namely Width and Height. It is
divided as Content Area and Decorations
(Title Bar and Borders).
There are five types of stages available −
Decorated
Undecorated
Transparent
Unified
Utility
You have to call the show() method to display the contents of a stage.
Scene
A scene represents the physical contents of a JavaFX application. It contains
all the contents of a scene graph. The class Scene of the
package javafx.scene represents the scene object. At an instance, the scene
object is added to only one stage.
4. You can create a scene by instantiating the Scene Class. You can opt for the
size of the scene by passing its dimensions (height and width) along with
the root node to its constructor.
Scene Graph and Nodes
A scene graph is a tree-like data structure (hierarchical) representing the
contents of a scene. In contrast, a node is a visual/graphical object of a scene
graph.
A node may include −
Geometrical (Graphical) objects (2D and 3D) such as − Circle, Rectangle,
Polygon, etc.
UI Controls such as − Button, Checkbox, Choice Box, Text Area, etc.
Containers (Layout Panes) such as Border Pane, Grid Pane, Flow Pane, etc.
Media elements such as Audio, Video and Image Objects.
The Node Class of the package javafx.scene represents a node in JavaFX, this
class is the super class of all the nodes.
A node is of three types −
Root Node − The first Scene Graph is known as the Root node.
Branch Node/Parent Node − The node with child nodes are known as
branch/parent nodes. The abstract class named Parent of the
package javafx.scene is the base class of all the parent nodes, and those parent
nodes will be of the following types −
o Group − A group node is a collective node that contains a list of children
nodes. Whenever the group node is rendered, all its child nodes are
rendered in order. Any transformation, effect state applied on the group
will be applied to all the child nodes.
o Region − It is the base class of all the JavaFX Node based UI Controls,
such as Chart, Pane and Control.
o WebView − This node manages the web engine and displays its contents.
Leaf Node − The node without child nodes is known as the leaf node. For
example, Rectangle, Ellipse, Box, ImageView, MediaView are examples of leaf
nodes.
It is mandatory to pass the root node to the scene graph. If the Group is
passed as root, all the nodes will be clipped to the scene and any alteration
in the size of the scene will not affect the layout of the scene.
5. Creating a JavaFX Application
To create a JavaFX application, you need to instantiate the Application class
and implement its abstract method start(). In this method, we will write the
code for the JavaFX Application.
Application Class
The Application class of the package javafx.application is the entry point of the
application in JavaFX. To create a JavaFX application, you need to inherit this
class and implement its abstract method start(). In this method, you need to
write the entire code for the JavaFX graphics
In the main method, you have to launch the application using
the launch() method. This method internally calls the start() method of the
Application class as shown in the following program.
Within the start() method, in order to create a typical JavaFX application, you
need to follow the steps given below −
Prepare a scene graph with the required nodes.
Prepare a Scene with the required dimensions and add the scene graph (root
node of the scene graph) to it.
Prepare a stage and add the scene to the stage and display the contents of the
stage.
6. Preparing the Scene
A JavaFX scene is represented by the Scene class of the package javafx.scene.
You can create a Scene by instantiating this class as shown in the following
code block.
While instantiating, it is mandatory to pass the root object to the constructor
of the scene class.
You can also pass two parameters of double type representing the height and
width of the scene as shown below.
Preparing the Stage
This is the container of any JavaFX application and it provides a window for
the application. It is represented by the Stage class of the package javafx.stage.
An object of this class is passed as a parameter of the start() method of
the Application class.
Using this object, you can perform various operations on the stage. Primarily
you can perform the following −
Set the title for the stage using the method setTitle().
Attach the scene object to the stage using the setScene() method.
Display the contents of the scene using the show() method as shown below.
7. Lifecycle of JavaFX Application
The JavaFX Application class has three life cycle methods, which are −
start() − The entry point method where the JavaFX graphics code is to be
written.
stop() − An empty method which can be overridden, here you can write the
logic to stop the application.
init() − An empty method which can be overridden, but you cannot create stage
or scene in this method.
In addition to these, it provides a static method named launch() to launch
JavaFX application.
Since the launch() method is static, you need to call it from a static context
(main generally). Whenever a JavaFX application is launched, the following
actions will be carried out (in the same order).
An instance of the application class is created.
Init() method is called.
The start() method is called.
The launcher waits for the application to finish and calls the stop() method.
Terminating the JavaFX Application
When the last window of the application is closed, the JavaFX application is
terminated implicitly. You can turn this behavior off by passing the Boolean
value False to the static method setImplicitExit() (should be called from a static
context).
You can terminate a JavaFX application explicitly using the
methods Platform.exit() or System.exit(int).