Chapter-3 JavaFx
Chapter-3 JavaFx
JavaFX Tutorial
Last Updated : 09 Jan, 2023
JavaFX is a Java library and a GUI toolkit designed to develop and facilitate Rich
Internet applications, web applications, and desktop applications. The most
significant perk of using JavaFX is that the applications written using this
library can run on multiple operating systems like Windows, Linux, iOS,
Android, and several platforms like Desktops, Web, Mobile Phones, TVs,
Tablets, etc. This characteristic of the JavaFX library makes it very versatile
across operating systems and different platforms.
Note: Rich Internet Applications are those web applications that allow
alike characteristics and expertise as that of desktop applications. These
applications contribute more satisfying visual experience to the users
when compared to the standard web applications.
https://www.geeksforgeeks.org/javafx-tutorial/ 1/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Need of JavaFX
Before JavaFX, developing the client base applications was a very complicated
and cumbersome task. Programmers and developers used to require several
libraries for adding various functionalities like media, UI controls, animations
and effects, 2D and 3D shapes, etc., in their applications. This issue was
resolved when JavaFX came into the picture, which changed the whole
scenario of the development of web applications by bringing in all the
peculiarities into one single library. Apart from this, the programmers can also
utilize all the existing perks of the older libraries like Java Swing and Advanced
Windowing Tool Kit.
JavaFX also displays a valuable collection of graphics and various media APIs,
which can further help in designing smooth applications. JavaFX also leverages
the improved Graphical Processing Unit (GUI) with the help of hardware-
accelerated graphics. If a developer wants to combine the graphics animations
https://www.geeksforgeeks.org/javafx-tutorial/ 2/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
and UI control in their applications, then they can use the various interfaces
provided by the JavaFX.
History of JavaFX
JavaFX was formerly developed by Chris Oliver. At that time, he was serving
for a company named See Beyond Technology Corporation. Initially, the JavaFX
project was recognized as Form Follows Functions (F3). This project was
designed with the aim of providing richer interfaces for developing GUI
applications. Later in June 2005, Sun Micro-systems took the F3 project and
changed its name from F3 to JavaFX.
Timeline of JavaFX
2005 – Sun Microsystems took over the See Beyond company in June 2005
and acquired the F3 project as JavaFX.
2007 – JavaFX was officially declared at Java One, a worldwide web
conference that is held yearly.
2008 – Net Beans integration with JavaFX was made open. The Java
Standard Development Kit for JavaFX 1.0 was also released in the same
year.
2009 – The next version of JavaFX was released, i.e., JavaFX 1.2, and the
support for JavaFX Mobile was also introduced. In the same year only,
Oracle Corporation also acquired Sun Microsystems.
2010 – JavaFX version 1.3 was released in 2010.
2011 – In 2011, JavaFX version 2.0 came out.
2012 – The support for JavaFX Mac OS for desktop was introduced.
2014 – The most advanced version of JavaFX, i.e., JavaFX 8, was released as
an indispensable part of Java on the 18th of March 2014.
Features of JavaFX
https://www.geeksforgeeks.org/javafx-tutorial/ 3/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
1. Java Library – JavaFX is a Java library, which allows the user to gain the
support of all the Java characteristics such as multithreading, generics,
lambda expressions, and many more. The user can also use any of the Java
editors or IDE’s of their choice, such as Eclipse, NetBeans, to write, compile,
run, debug, and package their JavaFX application.
4. Scene Builder – JavaFX also implements a tool named Scene Builder, which
is a visual editor for FXML. Scene Builder generates FXML mark-ups that can
be transmitted to the IDE’s like Eclipse and NetBeans, which further helps
the user to combine the business logic to their applications. The users can
also use the drag and drop design interface, which is used to design FXML
applications (just like the Drag & Drop feature of the Java Swing and
DreamWeaver Applications).
https://www.geeksforgeeks.org/javafx-tutorial/ 4/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
6. WebView – JavaFX applications can also insert web pages. To embed web
pages, Web View of JavaFX uses a new HTML rendering engine technology
known as WebKitHTML. WebView is used to make it possible to insert web
pages within a JavaFX application. JavaScript running in WebView can call
Java APIs and vice-versa.
8. CSS Styling – Just like websites use CSS for styling, JavaFX also provides
the feature to integrate the application with CSS styling. The users can
enhance the styling of their applications and can also improve the outlook of
their implementation by having simple knowledge and understanding of
CSS styling.
9. Rich set of APIs – JavaFX library also presents a valuable collection of APIs
that helps in developing GUI applications, 2D and 3D graphics, and many
more. This collection of APIs also includes all the characteristics of the Java
platform. Hence, working with this API, a user can access the specialties of
Java languages such as Generics, Annotations, Multithreading, and Lambda
Expressions, and many other features as well. In JavaFX, the popular Java
Collections library was also improved, and notions like lists and maps were
introduced. Using these APIs, the users can witness the variations in the
data models.
10. High-Performance media engine – Like the graphics pipeline, JavaFX also
possesses a media pipeline that advances stable internet multimedia
playback at low latency. This high-performance media engine or media
pipeline is based on a multimedia framework known as Gstreamer.
https://www.geeksforgeeks.org/javafx-tutorial/ 5/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Architecture of JavaFX
JavaFX has numerous built-in elements that are interconnected with each
other. JavaFX library comprises a valuable collection of APIs, classes, and
interfaces that are more than sufficient to produce rich internet applications
and GUI applications with intense graphics that can run consistently over
multiple platforms.
As we can see in the above figure that, JavaFX architecture comprises many
different components. These components are briefly described as follows:
1. JavaFX API – The topmost layer of JavaFX architecture holds a JavaFX public
API that implements all the required classes that are capable of producing a
full-featured JavaFX application with rich graphics. The list of all the
important packages of this API is as follows.
2. javafx.animation: It includes classes that are used to combine transition-
based animations such as fill, fade, rotate, scale and translation, to the
JavaFX nodes (collection of nodes makes a scene graph).
3. javafx.css − It comprises classes that are used to append CSS–like styling
to the JavaFX GUI applications.
4. javafx.geometry − It contains classes that are used to represent 2D figures
and execute methods on them.
https://www.geeksforgeeks.org/javafx-tutorial/ 6/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Root Node – A root node is a node that does not have any node as its
parent.
Leaf Node – A leaf node is a node that does not contain any node as its
children.
Branch Node – A branch node is a node that contains a node as its parent
and also has a node as its children.
https://www.geeksforgeeks.org/javafx-tutorial/ 7/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
10. Quantum Toolkit – Quantum Toolkit is used to connect prism and glass
windowing tool kits collectively and makes them prepared for the above layers
in the stack. In simple words, it ties Prism and GWT together and makes them
available to JavaFX.
11. Prism – The graphics of the JavaFX applications are based on the
hardware-accelerated graphics rendering pipeline, commonly known as Prism.
The Prism engine supports smooth JavaFX graphics that can be executed
swiftly when utilized with a backed graphics card or graphics processing unit
(GPU). In the situation where the system does not contain the graphic cards,
then the prism engine defaults to the software rendering stack. To interpret
graphics, a Prism practice −
13. WebView – JavaFX applications can also insert web pages. To embed web
pages, Web View of JavaFX uses a new HTML rendering engine technology
known as WebKitHTML. WebView is used to make it possible to insert web
pages within a JavaFX application. JavaScript appearing in WebView can call
https://www.geeksforgeeks.org/javafx-tutorial/ 8/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Java APIs and vice-versa. This element promotes different web technologies
like HTML5, CSS, JavaScript, DOM, and SVG. Using web view, we can execute
the HTML content from the JavaFX application and can also implement some
CSS styles to the user interface (UI) part of the application.
14. Media Engine – Like the graphics pipeline, JavaFX also possesses a media
pipeline that advances stable internet multimedia playback at low latency. This
high-performance media engine or media pipeline is based on a multimedia
framework known as Gstreamer. By applying the Media engine, the JavaFX
application can support the playback of audio and video media files. The
package javafx.scene.media covers all the classes and interfaces that can
provide media functionalities to JavaFX applications.
The foregoing were the components that constitute the architecture of JavaFX.
There are in total three life cycle methods of a JavaFX Application class. These
methods are –
start() – The start() method is the entry point method of the JavaFX
application where all the graphics code of JavaFX is to be written.
init() – The init() method is an empty method that can be overridden. In this
method, the user cannot create a stage or a scene.
https://www.geeksforgeeks.org/javafx-tutorial/ 9/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
stop() – The stop() method is an empty method that can also be overridden,
just like the init() method. In this method, the user can write the code to halt
the application.
Other than these methods, the JavaFX application also implements a static
method known as launch(). This launch() method is used to launch the JavaFX
application. As stated earlier, the launch() method is static, the user should call
it from a static method only. Generally, that static method, which calls the
launch() method, is the main() method only.
Whenever a user launches a JavaFX application, there are few actions that are
carried out in a particular manner only. The following is the order given in
which a JavaFX application is launched.
As soon as the last window of the JavaFX application is closed, the JavaFX
application is stopped implicitly. The user can turn off this function by passing
the Boolean value “False” to the static method setImplicitExit(). This method
should always be called from a static context only.
The user can also stop a JavaFX application explicitly by practicing any of the
two methods, Platform.exit() or System.exit(int).
JavaFX comprises all the major built-in UI components that help in developing
well-featured applications. These built-in UI components are not operating
system-dependent. In simple words, these controls do not depend on any of
the Operating systems like Windows, iOS, Android, etc. These built-in controls
are single-handedly ample to perform a whole implementation of the
applications.
https://www.geeksforgeeks.org/javafx-tutorial/ 10/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
The users can add numerous components to their applications to make them
look more advanced and rich in graphics. These segments are defined as
follows:
JavaFX 2D Shapes
Any geometrical shape that can be represented on the coordinate system using
two planes, which are X and Y planes, is known as a two-dimensional shape or
a 2D shape. Examples of 2D shapes are line, square, rectangle, circle, eclipse,
and many more.
JavaFX gives the flexibility to the user to add and create these 2D shapes in
their JavaFX applications. There are several classes in JavaFX API that are used
to execute 2D shapes in the JavaFX application. All these classes of 2D shapes
are part of the javafx.scene.shape package. The class named Shape is the root
class of all the 2-Dimensional shapes in JavaFX. There are many classes in this
package that contains various sorts of 2D shapes.
Steps to create a 2D shape are as follows:
Step 2: After the instantiation of the class and the object, the user should also
set the properties of the required shape using the setter functions. For
illustration purposes consider if a user wants to add a line to their JavaFX
application, then after instantiation, they should set the X and Y coordinates of
the starting and ending point of the line as defined below as follows:
line.setStartX(0);
line.setStartY(0);
line.setEndX(100);
line.setEndY(200);
https://www.geeksforgeeks.org/javafx-tutorial/ 11/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Step 3: After doing the above two steps, the third step for the user is to add
the object instantiated of the required shape to the group by declaring it as a
parameter of the constructor. For illustration purposes consider if in order to
get a line in the JavaFX application, the user should add the instantiated object
of line shape to the group, then it is defined as follows:
Types of 2D shapes
https://www.geeksforgeeks.org/javafx-tutorial/ 12/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
JavaFX Effects
JavaFX gives the flexibility to the user to add various effects such as bloom,
blur, and glow in their JavaFX applications. There are several classes in JavaFX
API that are used to execute these effects in the JavaFX application. All these
classes of effects are part of the javafx.scene.effects package. The class
named Effect is the root class of all the effects in JavaFX. There are many
classes in this package that contains various sorts of effects.
In order to add an effect to a Node, the user needs to sequentially follow these
steps as follows:
Step 1: The first step to applying an effect to a Node is to create all the nodes
in a JavaFX application by instantiating their individual classes. For illustration
purposes consider if a user wants to apply the glow effect to an image in their
application, then they should create an image node first by instantiating the
Image class. After that, they should set its view just as shown below as
follows:
// Creating an image
https://www.geeksforgeeks.org/javafx-tutorial/ 13/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
ng6.png”);
imgView.setX(100);
imgView.setY(100);
imgView.setFitHeight(200);
imgView.setFitWidth(400);
imgView.setPreserveRatio(true);
Step 2: After creating the image node and setting the image view, the user
should instantiate the class representing the required effect that is needed to
be applied to the node created. For illustration purposes of considering if the
user wants to apply the glow effect in their application, then they need to
instantiate the Glow class as shown below as follows:
Step 3: After instantiating the class of the required effect, the user needs to set
the attributes for the instantiated effect using its setter methods. For
illustration purposes consider as if the user can set the attributed of the Glow
effect using the below-shown method as follows:
https://www.geeksforgeeks.org/javafx-tutorial/ 14/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
glow.setLevel(0.9);
Step 4: After the above three steps, the user can finally apply the required
effect to the node using the setEffect() method.
For example – The user can set the glow effect to the image node by passing
the object of the Glow class to this method as follows –
imgView.setEffect(glow);
2. Glow – The Glow effect of JavaFX makes the provided input image glow.
Using this effect, the bright pixels of the input can be made brighter. The
class javafx.scene.effect.Glow represents the glow effect.
3. Bloom – Just like the Glow effect, in the Bloom effect also the pixels in some
parts of the node are produced to glow. The class javafx.scene.effect.Bloom
represents the bloom effect.
reflection effect.
6. Color Adjust – In JavaFX, a user can also alter the color of an image by
implementing the color adjust effect to it. This effect involves the
adjustment of the hue, saturation, brightness, and contrast on every pixel.
The class javafx.scene.effect.ColorAdjust represents the Color Adjust
effect.
8. Lighting – The effect which is used to resemble a light from a light source is
known as the Lighting effect. In JavaFX, there can be various sorts of light
sources specifically point, distant, and spot. The class
javafx.scene.effect.Lighting represents the Lighting effect.
JavaFX Texts
Just like several shapes and effects, a user can also create a node for applying
different texts in JavaFX. The text node is expressed by the class named Text,
which belongs to the javafx.scene.text package. This Text class includes
numerous characteristics to generate text in JavaFX and alter its appearance.
Another feature of the text class is that this class also inherits the Shape class
of JavaFX which belongs to the javafx.scene.shape package. Hence, in
extension to the characteristics of the text like font, alignment, line spacing,
text, etc., it also acquires the basic shape node properties such as strokeFill,
stroke, strokeWidth, strokeType, etc.
To create a text node, the user needs to follow certain steps as follows:
https://www.geeksforgeeks.org/javafx-tutorial/ 16/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Illustration: The class Text of the package javafx.scene.text denotes the text
node in JavaFX. A user can create a text by instantiating this class as given
below as follows:
Step 2: After instantiating the corresponding class of the text node, the second
step to create a text node in JavaFX is to set the properties of the required text
using the setter functions.
Illustration: After instantiating the Text class, the user needs to set the value
to this property using the setText() method as shown below as follows:
Step 3: After doing the above two steps, the third step for the user is to add
the object instantiated of the required text to the group by declaring it as a
parameter of the constructor.
Illustration: In order to get a text in the JavaFX application, the user should add
the instantiated object of the text node to the group.
The attributes of the JavaFX Text are represented in the table below.
https://www.geeksforgeeks.org/javafx-tutorial/ 17/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
https://www.geeksforgeeks.org/javafx-tutorial/ 18/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
JavaFX Animations
The transition which produces the myth of motion for an object is known as
animation. Animations can also be defined as the set of transformations
implemented on an object over the specified duration sequentially so that the
object can be displayed as it is in action. This can be achieved by the rapid
display of frames. In JavaFX, the package javafx.animation includes all the
classes that assist in applying the animations onto the nodes. All the classes of
this package extend the class javafx.animation.Animation. JavaFX provides the
classes for the transitions like RotateTransition, ScaleTransition,
TranslateTransition, FadeTransition, FillTransition, StrokeTransition, etc.
To apply a special animation to a target node, a user should follow the below-
given steps as follows:
Step 2: After creating the node, the next step is to instantiate the respective
animation class that is to be applied to the created node.
Step 3: After instantiating the respective animation class, in the step, the user
should set the properties of the animation.
rotate.setDuration(Duration.millis(2000));
rotate.setAxis(Rotate.Y_Axis);
https://www.geeksforgeeks.org/javafx-tutorial/ 19/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
rotate.setCycleCount(1000);
Step 4: In the next step, the user should set the target node on which the
animation will be applied.
rotate.setNode(sqr);
Step 5: After following all the above steps, the last step to apply an animation
to a target node is to play the applied animation using the play() method of the
Animation class.
rotate.play();
Scale Transition – The Scale transition is used to animate the scaling of the
selective node over a specific duration of time. If a user wants to implement
the scale transition to a node in their JavaFX application, then they should
instantiate the javafx.animation.ScaleTransition class.
https://www.geeksforgeeks.org/javafx-tutorial/ 20/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Fade Transition – The Fade transition is used to animate the opacity of the
node. In simple words, we can say that the fade transition keeps refreshing
the opacity of the node over a specific duration of time in order to reach the
final opacity value. If a user wants to implement the fade transition to a
node in their JavaFX application, then they should instantiate the
javafx.animation.FadeTransition class.
Fill Transition – The Fill transition is used to animate the selective node’s fill
color so that the fill color of the node varies between the two color values
over a specific duration of time. If a user wants to implement the fill
transition to a node in their JavaFX application, then they should instantiate
the javafx.animation.FillTransition class.
Path Transition – The Path transition is used to move the selective node
along the specified path over a specific duration of time. If a user wants to
implement the path transition to a node in their JavaFX application, then
they should instantiate the javafx.animation.PathTransition class.
JavaFX 3D Shapes
Any solid geometrical figure that can be represented on the coordinate system
using three planes, which are X, Y, and Z planes, is known as a three-
dimensional shape or a 3D shape. The main difference between the 3D shapes
and 2D shapes is that the 3D shapes always need to have an extra coordinate
https://www.geeksforgeeks.org/javafx-tutorial/ 21/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
There are many basic 3D shapes which we see on a daily basis such as
cylinders, spheres, boxes, cubes, pyramids, etc. Nevertheless, JavaFX
implements only 3 classes to create 3D shapes. These classes include spheres,
cylinders, and boxes. These classes are defined in the javafx.scene.shape
package. This package provides all the methods to deal with the 3D shapes.
The class Shape3D of the package javafx.scene.shape is the base class of all
the 3D shape classes in javafx.
As mentioned above, there are different classes for the different 3D shapes in
JavaFX. In order to create 3D shapes, the user just needs to instantiate those
classes. The user can use the following steps to generate proper shapes in
their JavaFX application.
1. The first step is to instantiate the class of the respective 3D shape which the
user wants to build.
https://www.geeksforgeeks.org/javafx-tutorial/ 22/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
For Example – If a user wishes to create a Box, then he should instantiate the
Box class of the JavaFX.
2. After instantiating the class, the next step is to set of properties of the
instantiated class.
For Example – If a user has instantiated the Box class of JavaFX, then he
should set the properties such as Height, width, and depth of the Box class.
box.setHeight(100.0);
box.setDepth(80.0);
box.setWidth(60.0);
3. After the above two steps, the user should set the camera for the scene. In
simple words, the user can set the camera to a particular position for the
camera view. However, this step is optional. A user can set the camera view
using the following code –
4. The last step to creating a 3D shape for the JavaFX application is to attach
the 3D shape to the Scene graph and set the relevant properties for the Scene
and stage.
For example: If a user has instantiated the box class, then he should link that
class with the Scene graph and the stage.
https://www.geeksforgeeks.org/javafx-tutorial/ 23/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
In JavaFX, there are basically 3 predefined 3D shapes classes that are available
for the user to use in their application. The following is a brief introduction of
various 3D shapes that are provided by JavaFX.
JavaFX Transformations
Rotation – Rotation is used to rotate the object from its source by a certain
angle. The class javafx.scene.transform.Rotate represents rotation.
https://www.geeksforgeeks.org/javafx-tutorial/ 24/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
2. After instantiating the class, the next step is to set of properties of the
instantiated class.
translate.setByX(400);
translate.setDuration(Duration.millis(1000));
translate.setCycleCount(500);
3. Set the transformation to the respective node and play the transformation
using the method play().
For Example: Let us assume that the user wants to translate a specific node
then after instantiating a transformation class and setting its value, the user
should set the node using the name of the node that should play the
translation.
https://www.geeksforgeeks.org/javafx-tutorial/ 25/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
JavaFX UI Controls
The UI controls are the elements that are truly exposed to the user for
intercommunication or information exchange. A layout represents the structure
of the UI elements on the screen. The response is the reaction of the UI
component when some event has happened on it.
In JavaFX, there are many UI controls that are used by the users in their
applications. Below is the list of usually used UI controls while the GUI is
designed using JavaFX.
https://www.geeksforgeeks.org/javafx-tutorial/ 26/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
CheckBox – Check Box is used to get the kind of information from the user
which contains various choices. The user marked the checkbox either on
(true) or off(false). The CheckBox in JavaFX is expressed by the
javafx.scene.control.CheckBox class.
Slider – Slider is used to provide a pane of options to the user in a graphical
form where the user needs to move a slider over the range of values to
select one of them. It is represented by the javafx.scene.control.Slider
class.
Menu – JavaFX provides a Menu class to implement menus. The menu is the
main component of any application. It is represented by the
javafx.scene.control.Menu class.
PasswordField – PasswordField is practiced to take the user’s password.
Whatever the user types in the password field, that is not displayed on the
screen to anyone. It is expressed by the javafx.scene.control.PasswordField
class.
ScrollBar – JavaFX Scroll Bar is used to implement a scroll bar to the user
so that the user can scroll down the application pages. It is represented by
the javafx.scene.control.ScrollBar class.
JavaFX Charts
In general, the chart can be defined as the graph or diagram which represents
the data in the form of symbols. Charts are mainly used to represent large
quantities of data and the relationship between parts of the data. We can
create different kinds of charts to represent different kinds of information. In
JavaFX, we can create the charts by using the classes provided by the package
javafx.scene.chart.
Types of charts
https://www.geeksforgeeks.org/javafx-tutorial/ 27/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
1. Pie Chart – In Pie Chart, the areas of a circle are used to define several
proportions of the complete information. In JavaFX, the class
javafx.scene.chart.PieChart is used to deal with the pie chart.
Types of Axis
In Charts, there can be different types of axis other than the X and Y-axis.
These are –
1. Category Axis – The category axis is used to describe the various sections
of the information. This axis is distinct from the value axis in the sense that
the specific values are not displayed on the category axis. In JavaFX, the
class javafx.scene.chart.CategoryAxis depicts the category axis. A user just
requires to instantiate this class in order to generate the category axis.
2. Number Axis – The Number axis is used to express the specific range of
values. In JavaFX, the class javafx.scene.chart.NumberAxis represents the
value axis. A user just requires to instantiate this class in order to generate
the Number axis.
https://www.geeksforgeeks.org/javafx-tutorial/ 28/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
In JavaFX, there are many charts that are used by the users in their
applications. Below is the list of usually used charts while the GUI is designed
using JavaFX.
Pie Chart – In Pie Chart, the areas of a circle are used to define several
proportions of the complete information. In JavaFX, the class
javafx.scene.chart.PieChart is used to deal with the pie chart.
Bar Chart – A bar chart is used to describe assorted data using rectangular
bars. The length of these bars represents the values. The bars can be
plotted both vertically and horizontally in the bar chart. In JavaFX, a Bar
chart is represented by a class named javafx.scene.chart.BarChart.
Area Chart – Area charts are applied to draw area-based charts. It plots the
area between the given set of points and the axis. In general, this chart is
used to differentiate two quantities. In JavaFX, an area chart is represented
by a class named javafx.scene.chart.AreaChart.
JavaFX Layouts
Layouts are the top-level container classes that describe the UI styles for
objects of the scene graph. The JavaFX layout can be seen as the father node to
all the separate nodes. JavaFX presents several layout panes that promote
various styles of layouts.
In JavaFX, Layout describes the process in which the elements are to be viewed
on the screen. It primarily establishes the scene-graph nodes. There are various
built-in layout panes in JavaFX that are HBox, VBox, StackPane, FlowBox,
AnchorPane, etc. Each Built-in layout is represented by a separate class that
requires to be instantiated in order to implement that specific layout pane.
https://www.geeksforgeeks.org/javafx-tutorial/ 29/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
1. The first step to creating a layout in JavaFX application is that the user
should instantiate the respective layout class,
2. After instantiating the respective class, the next step is to set the properties
for the layout.
root.setSpacing(20);
3. After the above two steps, the last step of creating a layout in the JavaFX
application is to add nodes to the layout object.
root.getChildren().addAll(<NodeObjects>);
In JavaFX, there are various JavaFX Layout classes that are available for the
user to use in their application. The following is a brief introduction of various
layouts that are provided by JavaFX.
HBox – The HBox layout is used to arrange all the nodes in the JavaFX
application in a particular horizontal row. In JavaFX, an HBox layout is
represented by a class named javafx.scene.layout.HBox.
BorderPane – The Border Pane layout is used to arrange the nodes in the
JavaFX application in the top, left, right, bottom, and center positions. In
https://www.geeksforgeeks.org/javafx-tutorial/ 30/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
GridPane – The Grid Pane layout is used to arranges the nodes in the
JavaFX application as a grid of rows and columns. This layout is very
convenient in designing forms using JavaFX. In JavaFX, a GridPane layout is
represented by a class named javafx.scene.layout.GridPane.
FlowPane – The flow pane layout is used to wrap all the nodes in a flow. A
horizontal flow pane wraps the components of the pane at its height, while
a vertical flow pane wraps the components at its width. In JavaFX, a
FlowPane layout is represented by a class named
javafx.scene.layout.FlowPane.
StackPane – The stack pane layout is used to arrange the nodes in the
JavaFX application on top of another just like in a stack. The node appended
first is ordered at the bottom of the stack and the next node is stored on top
of it. In JavaFX, a StackPane layout is represented by a class named
javafx.scene.layout.StackPane.
VBox – The VBox layout is used to arrange all the nodes in the JavaFX
application in a single vertical column. In JavaFX, a VBox layout is
represented by a class named javafx.scene.layout.VBox.
In JavaFX, An event occurs whenever the user interacts with the application
nodes. There are multiple references by using which, the user can create the
event. For example, the user can make use of a mouse or it can press any
https://www.geeksforgeeks.org/javafx-tutorial/ 31/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
button on the keyboard or it can scroll any page of the application in order to
generate an event. Hence, we can say that the events are fundamentally the
announcements that tell us that something has happened at the user’s end. A
perfect Application is the one that takes the least amount of time in handling
the events.
In JavaFX, events are primarily used to inform the application regarding the
actions chosen by the user. JavaFX implements the tool to achieve the events,
route the event to its target, and granting the application to handle the events.
JavaFX presents the class javafx.event.Event which includes all the subclasses
describing the varieties of events that can be created in JavaFX. Any event is an
instance of the class Event or any of its subclasses.
Types of Events
In general, the JavaFX events are principally grouped into the subsequent types
–
https://www.geeksforgeeks.org/javafx-tutorial/ 32/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
We need to configure JavaFX in our IDE in order to use it for which here we are
illustrating with the help of Eclipse IDE as it is widely used by Java developers.
Step 1: Go to ‘Help’ section on by clicking the top right bar and further go into
‘Eclipse Marketplace’ as shown in below media.
https://www.geeksforgeeks.org/javafx-tutorial/ 33/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Step 2: Now write initials ‘fx’ and press enter. Below window will pop-up then
install the e(fx)clipse 3.6.0 as shown below which will appear in top search as
later on continue installing by accepting terms and conditions and press ‘Next’.
It will take a little bit of time.
Step 3: Restart the IDE and now open new ‘File’ new –> ‘Others’ and here you
will find JavaFX plugin which earlier was not present here as depicted below as
follows:
https://www.geeksforgeeks.org/javafx-tutorial/ 34/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Just likely naming normal project so do create JavaFX project and choose
settings as per requirements, further click on ‘Next‘ later on ‘Finish‘.
https://www.geeksforgeeks.org/javafx-tutorial/ 35/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
You will see this window where the keynote is that our IDE(eclipse) does not
know where the JavaFX is as of now we only have set up JavaFX to work in
eclipse only but have not been downloaded JavaFX.
Go to Windows –> Preferences and there type user and go to User Libraries.
Here add a new user library and name t relevantly not necessarily to named
JavaFX or retaining some previous counter names.
to
https://www.geeksforgeeks.org/javafx-tutorial/ 36/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Step 7: Now go to ‘Add External JAR’ and do copy the path retained above
where JavaFX is installed in the local directory and select all of them as shown
below as follows:
Step 8: Resolve unwanted errors that pop up by right-clicking the project and
go to Build path –> Configure Build Path. Now go to Library –> Classpath
and Add library. here you will find all user library and the name will pop up
which was given earlier to the library. Click on Finish –> Apply –> Apply and
Close and you will see all the errors are gone by now.
Go to Run –> Java Application –> Main –> Arguments. keynote is here you
need to pass the argument as shown in the image below but the path varies as
per where you have installed JavaFX.
https://www.geeksforgeeks.org/javafx-tutorial/ 37/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
After this one can start off with JavaFX operations as a window will be
popped up by now. Cheers!
The performance of both Java Swing and Java FX is supported in the graphics
business. However, both of them are very different from each other. Some of
the key differences between Java Swing and Java FX are given below –
Swing is the standard toolkit for Java JavaFX grants platform assistance for
developers in creating GUI designing desktop applications.
Swing is a legacy library that fully JavaFX has UI components that are still
features and provides pluggable UI growing with a further advanced look
https://www.geeksforgeeks.org/javafx-tutorial/ 38/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Swing has a UI component library and JavaFX has several components built
acts as a legacy over Swing.
Swing has assistance for MVC, but it is JavaFX support is really favorable with
not uniform across a component. MVC.
https://www.geeksforgeeks.org/javafx-tutorial/ 39/46
1/20/25, 9:52 AM JavaFX Tutorial - GeeksforGeeks
Swing has basic controls like buttons, JavaFX has many eye-catching controls
checkboxes, and bandboxes. that Swing doesn’t have
These are the main points of difference between JavaFX and Java Swing
There are many real-world applications that were made using JavaFX. Some of
the applications and their area of use are given below –
https://www.geeksforgeeks.org/javafx-tutorial/ 40/46