Introduction to JavaFX
Why JavaFX
JavaFX is a new framework for developing Java GUI Programs
Graphical functionality is provided by the library, no need to write your own
Some Java History
Ancient code: AWT
Until Java 7: Swing (Will never die, most current application still use it)
Java 8 and later: JavaFX
JavaFX Architecture
JavaFX API
javafx.animation − Contains classes to add transition based animations such as fill,
fade, rotate, scale and translation, to the JavaFX nodes.
javafx.application − Contains a set of classes responsible for the JavaFX application
life cycle.
javafx.css − Contains classes to add CSS–like styling to JavaFX GUI applications.
javafx.event − Contains classes and interfaces to deliver and handle JavaFX events.
javafx.geometry − Contains classes to define 2D objects and perform operations on
them.
javafx.stage − This package holds the top level container classes for JavaFX
application.
javafx.scene − This package provides classes and interfaces to support the scene
graph.
Scene Graph
A Scene Graph is the starting point of the construction of the GUI Application. It holds
the (GUI) application primitives that are termed as nodes.
Geometrical (Graphical) objects − (2D and 3D) such as circle, rectangle, polygon, etc.
UI controls − such as Button, Checkbox, Choice box, Text Area, etc.
Containers − (layout panes) such as Border Pane, Grid Pane, Flow Pane, etc.
Media elements − such as audio, video and image objects.
Prism
Prism is a high performance hardware–accelerated graphical pipeline that is used to
render the graphics in JavaFX. It can render both 2-D and 3-D graphics.
GWT (Glass Windowing Toolkit)
As the name suggests, GWT provides services to manage Windows, Timers, Surfaces
and Event Queues. GWT connects the JavaFX Platform to the Native Operating
System.
Quantum Toolkit
It is an abstraction over the low-level components of Prism, Glass, Media Engine, and
Web Engine. It ties Prism and GWT together and makes them available to JavaFX.
WebView
Using JavaFX, you can also embed HTML content in to a scene graph. WebView is the
component of JavaFX which is used to process this content.
Media Engine
The JavaFX media engine is based on an open-source engine known as a Streamer.
This media engine supports the playback of video and audio content.
JavaFX Application Structure
Stage
A stage (a window) contains all the objects of a JavaFX application.
It is represented by Stage class of the package javafx.stage.
The primary stage is created by the platform itself.
The created stage object is passed as an argument to the start() method of
the Application class.
You have to call the show() method to display the contents of a stage.
Scene
A scene represents the physical contents of a JavaFX application.
It contains all the contents of a scene graph.
The class Scene of the package javafx.scene represents the scene object.
At an instance, the scene object is added to only one stage.
Scene Graph and Nodes
A scene graph is a tree-like data structure (hierarchical) representing the contents of a
scene.
In contrast, a node is a visual/graphical object of a scene graph.
package application;
rect.setX(20);
import javafx.application.Application;
rect.setY(20);
import javafx.scene.Group;
rect.setWidth(100);
import javafx.scene.Scene;
rect.setHeight(100);
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
rect.setArcHeight(35);
import javafx.stage.Stage;
rect.setArcWidth(35);
public class Shape_Example extends Application{
rect.setFill(Color.RED);
group.getChildren().addAll(rect);
@Override
Scene scene = new Scene(group,200,300,Color.GRAY);
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(scene);
// TODO Auto-generated method stub primaryStage.show();
primaryStage.setTitle("Rectangle Example"); }
Group group = new Group(); public static void main(String[] args) {
Rectangle rect=new Rectangle(); launch(args);
}
}
JavaFX HelloWorld Example
//import javafx.scene.control.Button, not java.awt.Button!!!!!
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
// Override the start method in the Application class
@Override
public void start(Stage primaryStage) {
// Create a scene and place a button in the scene
Button btOK = new Button("OK");
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene
primaryStage.show();
}
}
package application;
import javafx.application.Application;
RadioButton button3 = new RadioButton("option 3");
import javafx.scene.Scene; RadioButton button4 = new RadioButton("option 4");
import javafx.scene.control.RadioButton; }
}
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
button1.setToggleGroup(group);
import javafx.stage.Stage;
button2.setToggleGroup(group);
public class RadioButtonTest extends Application {
button3.setToggleGroup(group);
public static void main(String[] args) {
button4.setToggleGroup(group);
launch(args);
VBox root=new VBox();
}
root.setSpacing(10);
@Override
root.getChildren().addAll(button1,button2,button3,butt
public void start(Stage primaryStage) throws Exception {
on4);
// TODO Auto-generated method stub Scene scene=new Scene(root,400,300);
ToggleGroup group = new ToggleGroup(); primaryStage.setScene(scene);
RadioButton button1 = new RadioButton("option 1"); primaryStage.setTitle("Radio Button Example");
RadioButton button2 = new RadioButton("option 2"); primaryStage.show();
}
}
package application; //Handling KeyEvent for textfield 1
import javafx.application.Application; tf1.setOnKeyPressed(new EventHandler<KeyEvent>()
import javafx.event.EventHandler; {
import javafx.scene.Group; @Override
import javafx.scene.Scene; public void handle(KeyEvent key) {
import javafx.scene.control.TextField; // TODO Auto-generated method stub
import javafx.scene.input.KeyEvent; tf2.setText("Key Pressed :"+" "+key.getText());
import javafx.scene.paint.Color; }
import javafx.stage.Stage; });
public class JavaFX_KeyEvent extends Application{ //setting group and scene
@Override Group root = new Group();
public void start(Stage primaryStage) throws Exception { root.getChildren().addAll(tf2,tf1);
// TODO Auto-generated method stub Scene scene = new Scene(root,500,200,Color.WHEAT)
;
//Creating TextFields and setting position for them
primaryStage.setScene(scene);
TextField tf1 = new TextField();
primaryStage.setTitle("Handling KeyEvent");
TextField tf2 = new TextField();
primaryStage.show();
tf1.setTranslateX(100);
}
tf1.setTranslateY(100);
public static void main(String[] args) {
tf2.setTranslateX(300);
launch(args); } }
tf2.setTranslateY(100);
}
Layout Panes
JavaFX provides many types of panes for organizing
nodes in a container.
15