[go: up one dir, main page]

0% found this document useful (0 votes)
19 views15 pages

Unit5 cs3391

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Unit5 cs3391

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT -5

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:

Here, we notice two main containers:

 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

JavaFX Controls Hierarchy

The javafx.application.Application class has the following lifecycle methods:

 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.

The static launch() method starts the JavaFX application.


Simple Hello World Application using JAVA FX

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!");
}
});

StackPane root = new StackPane();


root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Here are the important things to know about the basic structure of a JavaFX application:

 The main class for a JavaFX application extends


the javafx.application.Application class. The start() method is the main entry point

for all JavaFX applications.

 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

container for all content.

 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

print a message when the button is pressed.


Controls IN JAVA FX :

JAVA FX CONTROLS
Button
Event Handling in JAVA FX
NOTE :DO REFER TO ALL THE PROGRAMS DONE IN THE CLASS

You might also like