Updated April 17, 2023
Introduction to JavaFX API
In order to build rich client applications, a library known as JavaFX is used which offers an API for creating, testing, debugging and deploying GUI applications that run on nearly all devices that have the support of Java. That is, it operates in a consistent manner across all platforms. Moreover, these application programming interfaces (API) helps in such a way that it acts as a friendly alternative for JVM languages (Java Virtual Machine) languages like Scala as well as JRuby. Also, it accesses several capabilities of the native system and help in connection to middleware applications which are server-based.
Architecture of JavaFX API
Below is the architecture of JavaFX API.
In the GUI application, a scene graph is considered as the starting point of its construction. It consists of all the application primitives known as a node. Prism in this architecture is a higher performance hardware-accelerated graphical pipeline which helps in rendering the JavaFX graphics. Here, both two dimensional and three-dimensional graphics can be rendered.
GWT offers services for managing surfaces, windows, timers and event queues. It connects the platform of JavaFX API and native OS. WebView is the JavaFX component that helps in processing the content using a technology known as Web Kit. It is an internal web browser engine which is open-source. This component provides several web technologies such as HTML5, DOM, JavaScript, CSS and SVG. The media engine in JavaFX is based on an engine called a streamer which is open-source. This engine supports the both the video playback and audio content.
Packages in JavaFX API
The important JavaFX API packages include:
- javafx.animation: A set of classes will be provided that is for transition-related animations.
- javafx.application: Application life-cycle classes of the package will be provided.
- javafx.beans: Interfaces that explain the observability generic form is explained in this package.
- javafx.beans.binding: Binding characteristics are explained in this package.
- javafx.beans.property: Read-only and writable properties along with numerous implementations are provided in this package.
- javafx.beans.value: ObservableValue interface and WritableValue interface, along with all the sub-interfaces, are provided in this package.
- javafx.collections: All the JavaFX collections and their utilities are available in this package.
- javafx.concurrent: A set of classes will be provided for the javafx.task.
- javafx.css: An API that makes the properties stylable with the help of CSS and supports pseudo-class state is provided in this package.
- javafx.embed.swing: A set of classes that helps in using JavaFX within the swing applications is provided.
- javafx.embed.swt: A set of classes that helps in using JavaFX within the SWT applications is provided.
- javafx.event: A basic framework is provided for the FX events, their delivery as well as handling.
- javafx.fxml: In order to load a hierarchy of object from markup, this package contains all the classes.
- javafx.geometry: This package consists of 2D classes set that define as well as perform operations on objects that relate to 2-D geometry.
- javafx.print: This package offers the JavaFX printing API public classes.
- javafx.scene: This package contains the base classes’ core set for the scene graph API in JavaFX.
- javafx.scene.canvas: This package offers a class set for canvas which is a rendering API’s immediate mode style.
- javafx.scene.chart: This offers several chart components, which is very useful for data visualization.
- javafx.scene.control: User interface controls in JavaFX are the specialized nodes which are available in the scene graph of JavaFX. It is especially appropriate for reusing several application contexts.
- javafx.scene.effect: This package offers different classes for the attachment of graphical filter effects to the nodes of the JavaFX scene graph.
Examples of JavaFX API
Given below are the examples of JavaFX API:
Example #1
JavaFX program that displays a background color with the help of API packages.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
import javafx.scene.control.* ;
import javafx.scene.layout.* ;
import javafx.stage.Stage ;
import javafx.scene.paint.*;
import javafx.scene.shape.Circle;
//main class
public class BackgroundClassProgram extends Application {
// application launches at this point
public void start(Stage st)
{
// set the title for the stage
st.setTitle("Sample creation of background. . .") ;
//Circle c creation
Circle c = new Circle();
//Set the properties of circle
c.setCenterX(311.0f);
c.setCenterY(126.0f);
c.setRadius(112.0f);
/// create a hbox
HBox hb = new HBox(c);
// spacing set
hb.setSpacing(11);
// alignment setting for the hbox
hb.setAlignment(Pos.CENTER);
// creation of scene
Scene sc = new Scene(hb, 290, 280) ;
// creation of background fill
BackgroundFill bf = new BackgroundFill(Color.RED ,
CornerRadii.EMPTY , Insets.EMPTY) ;
// creation of background bg
Background bg = new Background(bf);
// setting the background
hb.setBackground(bg);
// set the scene
st.setScene(sc);
//display the result
st.show();
}
// Main Method
public static void main(String args[])
{
// application launches here
launch(args);
}
}
Output:
For every program, we have to first import the necessary packages and classes. In this program also, all the necessary classes are imported. Then only the appropriate functions can be used for the display of background colours.
Example #2
JavaFX program that displays a timer with the help of API packages.
Code:
import java.util.Timer ;
import java.util.TimerTask ;
public class TimerProgramSample {
public static void main(String[] args)
{
// timer notification
System.out.println("Timer starts now...") ;
// object creation for timer
Timer tmr = new Timer() ;
//timer schedule
tmr.schedule(new TimerTask()
{
//run method override
@Override
//run method
public void run()
{
//notifying about timer
System.out.println("Timer starts. . . .") ;
}
}, 3000) ;
//repeats each 20second
Timer tt = new Timer() ;
//repeating timer schedule
tt.scheduleAtFixedRate(new TimerTask()
{
//run method override
@Override
public void run()
{
System.out.println("Timer is working fine. . . .") ;
}
}, 0, 3000) ;
}
}
Output:
All the necessary classes and packages are imported at the beginning of the program. If any of the necessary packages are absent, it can create errors in the program. On executing the code, a repeating timer will be displayed, which can be terminated by clicking the red square box.
Conclusion
JavaFX is a library that is used for building GUI related applications. It offers an API in order to design GUI applications that run on nearly all devices that have the support of Java. In this article, different aspects of JavaFX API such as architecture, packages and examples are shown in detail.
Recommended Articles
This is a guide to JavaFX API. Here we discuss the introduction, architecture, packages and examples of JavaFX API, respectively. You may also have a look at the following articles to learn more –