Unit5 cs3391
Unit5 cs3391
JavaFX is a library for building rich client applications with Java. It provides an API for designing GUI
applications that run on almost every device with Java support.
JavaFX is a set of graphics and media packages that enables developers to design, create, test,
debug, and deploy rich client applications that operate consistently across diverse platforms.
JavaFX stands for Java "special EFF-ECTS" as FX is normally the abbreviation given to special effects
mostly sound or visual.
JavaFX API
In Java 8, 9, and 10 no additional setup is necessary to start working with the JavaFX library.
JavaFX Architecture
JavaFX uses hardware accelerated graphics pipeline for the rendering, known as Prism. What's
more, to fully accelerate the graphics usage, it leverages either software or hardware rendering
mechanism, by internally using DirectX and OpenGL.
JavaFX has a platform dependent Glass windowing toolkit layer to connect to the native
operating system. It uses the operating system's event queue to schedule thread usage. Also, it
asynchronously handles windows, events, timers.
he Media and Web engines enable media playback and HTML/CSS support.
Let's see what the main structure of a JavaFX application looks like:
Stage is the main container and the entry point of the application. It represents the main
window and passed as an argument of the start() method.
Scene is a container for holding the UI elements, such as Image Views, Buttons, Grids,
TextBoxes.
The Scene can be replaced or switched to another Scene. This represents a graph of hierarchical
objects, which is known as a Scene Graph. Each element in that hierarchy is called a node. A single
node has its ID, style, effects, event handlers, state.
JavaFX Lifecycle
init() – is called after the application instance is created. At this point, the JavaFX API isn't
ready yet, so we can't create graphical components here.
start(Stage stage) – all the graphical components are created here. Also, the main thread for
the graphical activities starts here.
stop() – is called before the application shutdown; for example, when a user closes the main
window. It's useful to override this method for some cleanup before the application termination.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
Here are the important things to know about the basic structure of a JavaFX application:
A JavaFX application defines the user interface container by means of a stage and a scene.
The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the
In JavaFX, the content of the scene is represented as a hierarchical scene graph of nodes.
In this example, the root node is a StackPane object, which is a resizable layout node.
This means that the root node's size tracks the scene's size and changes when the stage is
resized by a user.
The root node contains one child node, a button control with text, plus an event handler to
JAVA FX CONTROLS
Button
Event Handling in JAVA FX
NOTE :DO REFER TO ALL THE PROGRAMS DONE IN THE CLASS