Java Tutorial



Java Tutorial



JavaFX is a powerful framework for building Java applications with rich graphical user interfaces (GUIs). It provides a set of libraries and tools that enable developers to create cross-platform desktop, mobile, and web applications. JavaFX is designed to be user-friendly, highly customizable, and capable of delivering visually stunning experiences to end-users.


Are you looking to develop stunning, interactive user interfaces for your Java applications? Look no further than this comprehensive JavaFX tutorial. We will dive into the world of JavaFX, exploring its features, architecture, and examples. 

Whether you're a beginner or an experienced Java developer, this guide will equip you with the knowledge and skills to leverage JavaFX and build visually appealing applications.

What is JavaFX?

JavaFX is a Java library that allows developers to create rich, interactive, and visually appealing applications. It provides a comprehensive set of components, controls, layouts, and effects to build modern UIs. With JavaFX, you can create desktop applications and mobile apps, even embed them into web pages using JavaFX WebView.

For example, let's consider a simple JavaFX application that displays a "Hello, World!" message in a window:

Output: A window displaying the text "Hello, World!".

In this example, we extend the `Application` class provided by JavaFX and override the `start` method. We create a `Label` control with the text "Hello, World!" and add it to a `Scene`. Finally, we set the scene on the primary stage and show it.

Output: A window displaying the text "Hello, World!" with a width of 300 pixels and a height of 200 pixels.

In this example, a JavaFX application displays a label with the text "Hello, World!" in a window. The label is set as the root node of the scene, and the scene is created with a width of 300 pixels and a height of 200 pixels. The primary stage is set with the title "HelloWorldApp", and the scene is displayed in a window with the specified dimensions.

History of JavaFX

JavaFX, short for Java Graphics Framework, was introduced by Sun Microsystems (now owned by Oracle Corporation) as a successor to the ageing Swing toolkit. It was first released in 2008 as part of Java SE (Standard Edition) 8, and it has since evolved into a robust platform for building modern and responsive applications. Let's take a brief look at its evolution:

- 2008: JavaFX 1.0 was released, introducing a new set of UI controls, graphics, and multimedia capabilities.

- 2010: JavaFX 2.0 was launched, marking a major overhaul of the framework with improved performance, a new scene graph API, and better integration with Java.

- 2014: JavaFX 8 was released, providing enhanced support for web and mobile applications, improved 3D graphics, and better styling capabilities.

- 2018: JavaFX 11 was released as a standalone module, decoupled from the JDK (Java Development Kit), allowing for separate releases and faster updates.

- 2020: JavaFX 14 was released, bringing additional features such as a new HTTP client API, improved CSS support, and new UI controls.

- 2022: JavaFX 19 was released, with HTTP Live Streaming added as coded support. 

Features of JavaFX

JavaFX offers a wide range of features that make it a compelling choice for developing GUI-based applications:

1. Rich UI Controls: JavaFX provides a comprehensive set of UI controls, including buttons, checkboxes, lists, tables, and more. These controls are highly customizable and offer a consistent look and feel across different platforms.

2. Scene Graph: JavaFX utilizes a powerful scene graph API, allowing developers to create and manage complex scenes composed of nodes. This scene graph enables efficient rendering, layout, and event handling.

3. CSS Styling: JavaFX supports Cascading Style Sheets (CSS), enabling developers to apply styles and themes to their applications. This separation of UI design and logic enhances maintainability and allows easy customization.

4. Media Support: JavaFX supports playing audio and video files, capturing webcam input, and rendering rich media content. It provides a seamless multimedia experience within your applications.

5. Hardware Acceleration: JavaFX leverages hardware acceleration to provide smooth animations and fluid user interactions. It employs a graphics pipeline that handles rendering and compositing operations. This ensures optimal performance, especially when dealing with graphically intensive applications.

6. Web Integration: JavaFX includes the WebView component, which allows developers to embed web content within their applications. This feature enables the creation of hybrid applications that combine web technologies with native JavaFX capabilities.

These are just a few of the many features that JavaFX offers. Its extensive toolkit empowers developers to create visually stunning and highly interactive applications.

Architecture of JavaFX

JavaFX follows a layered architecture, separating concerns and promoting modularity. The key components of the JavaFX architecture, apart from the scene graph feature, which was discussed earlier, are:

JavaFX Animations: JavaFX lets you design fluid, dynamic user interface animations. Keyframes, transitions, and timelines can animate UI element position, size, opacity, and rotation.

JavaFX 3D Shapes: This allows you to construct and manipulate 3D shapes and objects. Create cubes, spheres, cylinders, and custom meshes in 3D space, add textures and materials, and control their position, rotation, and scale.

JavaFX transformations: JavaFX lets you move, rotate, scale, and skew UI elements. User interfaces can be animated via translation, rotation, scaling, shearing, and mirroring.

JavaFX UI Controls: It includes buttons, labels, text fields, checkboxes, sliders, and more. To design interactive, user-friendly interfaces, you can style, establish event handlers, and connect data to these controls. 

JavaFX Charts: JavaFX can construct line, bar, pie, and scatter charts. These charts can be filled with data, customised, and interactive to display data in your apps.

JavaFX layouts: It also provides layout containers to help you arrange and position UI elements. It supports FXML (JavaFX Markup Language), which allows developers to define UI layouts declaratively using XML-based syntax. FXML files can be loaded and processed to construct the scene graph. Layouts like `VBox`, `HBox`, `BorderPane`, and `GridPane` let you organise controls in a scene.

JavaFX Event Handling: JavaFX's event handling technology handles user interactions and events. Event handlers can be attached to UI controls like buttons, mouse movements, and key presses to enable user interaction and interactivity in your apps.

This modular architecture of JavaFX promotes code organization, reusability, and maintainability.

LifeCycle of a JavaFX Application

Understanding the life cycle of a JavaFX application is essential for developing robust and well-structured code. The life cycle consists of several phases:

1. Initialization: In this phase, JavaFX initializes the JavaFX runtime environment. It sets up essential resources and initializes the toolkit.

2. Start: The start phase is where the application's main entry point is called. Here, you can create the initial scene and set it on the primary stage.

3. Running: The application enters the running state once the start phase completes. During this phase, user interactions occur, events are processed, and the application remains responsive.

4. Stop: When the application is about to exit, the stop phase is triggered. This is where you can perform cleanup tasks, release resources, and gracefully terminate the application.

Understanding the life cycle allows you to manage resources efficiently and handle application events appropriately.

JavaFX 2D Shapes

JavaFX provides a rich set of 2D shape classes that enable the creation of complex graphics and visualizations. These shape classes include rectangles, circles, polygons, lines, and more. Here's an example of drawing a rectangle using JavaFX:

Output: A blue rectangle displayed in a window.

In this example, we create a Rectangle shape with a position of (100, 100) and dimensions of 200x150. We set its fill color to blue and add it to a Pane container. The pane is then set as the scene's root, displayed in a window.

JavaFX Effects

JavaFX provides a variety of effects that can be applied to nodes to enhance their visual appearance. These effects include blurs, shadows, reflections, and more. Let's look at an example of applying a drop shadow effect to a button:

Output: A button with a drop shadow effect displayed in a window.

In this example, we create a `Button` with the text "Click Me" and set its font size. We then create a `DropShadow` effect and apply it to the button using the `setEffect` method. The button is added to a `StackPane`, which is set as the scene's root and displayed in a window.

Properties of JavaFX Texts

JavaFX provides a comprehensive set of properties for styling and manipulating text. These properties include font, size, color, alignment, and more. By leveraging these properties, you can create visually appealing and customized text elements. Here's an example:

Output: A label with customized text properties displayed in a window.

In this example, we create a `Label` with the text "Welcome to JavaFX". We set its font to Arial, make it bold and italic, and set the font size to 24. We also change the text color to red and align the text in the center. The label is then added to the scene and displayed in a window.

    public static void main(String[] args) {

Output: A 3D cube displayed in a window.

In this example, we create a `Box` shape with dimensions of 100x100x100. We set its translation in the x, y, and z axes and apply a blue material to the box. The box is added to a `Group`, which is then set as the root of the scene. We add a perspective camera to enable 3D rendering and apply rotation transforms to the box for a visual effect. Finally, the scene is displayed in a window.

JavaFX v/s Java Swing

JavaFX and Java Swing are both UI toolkits for developing desktop applications in Java. While they serve similar purposes, there are significant differences between them. Here's a brief comparison:

- JavaFX is the newer technology with a more modern and visually appealing look and feel than Swing.

- JavaFX provides built-in support for 2D and 3D graphics, animations, and multimedia, while Swing primarily focuses on UI components.

- JavaFX uses a scene graph-based rendering approach, offering better performance and flexibility, whereas Swing uses a lightweight component-based model.

- JavaFX supports CSS styling for UI customization, whereas Swing uses its own look and feel (such as the default "Metal" look).

- JavaFX has better integration with modern UI technologies like CSS3, HTML5, and JavaScript, making it more suitable for rich internet applications.

- JavaFX has better support for touch and mobile devices compared to Swing.

In summary, JavaFX is a more modern and versatile UI toolkit with enhanced graphics capabilities and better integration with modern technologies, while Swing is a mature and stable UI toolkit with a larger ecosystem and backwards compatibility with older Java versions.

Real-world Applications made using JavaFX

JavaFX has been widely used to develop desktop applications in various domains. Here are some examples of real-world applications built using JavaFX:

1. SceneBuilder: A visual layout tool for designing JavaFX user interfaces.

2. JFoenix: A JavaFX material design library for creating modern and visually appealing applications.

3. Medusa: A JavaFX-based library for creating custom gauges and controls for IoT applications.

4. Gluon Mobile: A framework for building mobile applications using JavaFX for the front-end and GraalVM for native deployment.

5. TornadoFX: A lightweight and intuitive JavaFX framework for building desktop applications.

6. jMonkeyEngine: A powerful Java-based 3D game engine that utilizes JavaFX for its user interface.

These examples demonstrate the versatility and capabilities of JavaFX in developing a wide range of applications, including UI design tools, material design applications, IoT applications, mobile applications, desktop applications, and game engines.


JavaFX is a robust and adaptable platform for creating rich and interactive Java desktop applications. It includes 2D and 3D images, animations, UI controls, charts, layouts, event management, and other features. It is an attractive alternative for designing cross-platform desktop applications due to its modern look and feel.

JavaFX delivers a user-friendly and flexible framework for designing aesthetically beautiful and feature-rich applications, whether you are a beginner or an experienced Java developer. So, plunge into the world of JavaFX, investigate its capabilities, and begin creating your next desktop masterpiece.


1. What does JavaFX stand for?

JavaFX stands for special effects in Java. It is a software platform for creating and delivering rich internet applications (RIAs) that can run across various devices.

2. How to download JavaFX separately?

Since JavaFX is now bundled with the Java Development Kit (JDK) starting with JDK 11, you don't need to download it separately. Simply download the JDK from the official Oracle website, and JavaFX will be included.

3. What language is JavaFX?

JavaFX Script™ or commonly referred to as JavaFX, is a statically typed and declarative scripting language. It has first-class functions, declarative syntax, list comprehensions, and dependency-based evaluation performed incrementally.

Leave a Reply

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