JavaFX 2024
JavaFX 2024
Introduction
A graphical user interface (GUI) presents a user-friendly mechanism for
interacting with an app.
GUIs are built from GUI components— called controls or widgets.
Introduction
JavaFX is a Java library used to develop Desktop applications as well as
Rich Internet Applications (RIA).
Like Swing, JavaFX also provides its own components and doesn't
depend upon the operating system.
Introduction
JavaFX has 3 parts
• GUI builder called SceneBuilder allows drag-and-drop manipulation of
widgets.
1.Import javafx.application.Application.
start() –Entry point method of the JavaFX application where all the
graphics code of JavaFX is to be written.
stop() – In this method, the user can write the code to halt the application.
start() – The start() method is the entry point method of the JavaFX
application where all the graphics code of JavaFX is to be written.
init() – The init() method is an empty method that can be overridden. In this
method, the user cannot create a stage or a scene.
Whenever a user launches a JavaFX application:
The Node Class of the package javafx.scene represents a node in JavaFX, this
class is the super class of all the nodes.
Scene Graph
• A Scene Graph is the starting point of the development of any of the GUI
Applications.
• In JavaFX, all the GUI Applications are made using a Scene Graph only.
Branch Node/Parent Node: The abstract class named Parent of the package
javafx.scene is the base class of all the parent nodes, and those parent nodes will be of
the following types –
• Group: A group node is a collective node that contains a list of children nodes.
Whenever the group node is rendered, all its child nodes are rendered in order.
Any transformation, effect state applied on the group will be applied to all the
child nodes.
In JavaFX, a group is a container component that does not apply any
special layout for the children.
• Region: It is the base class of all the JavaFX Node based UI Controls, such as
Chart, Pane and Control.
• WebView: This node manages the web engine and displays its contents.
set the webview to the scene with preferred height and preferred
width
Each visual element in the scene graph is a node, which defines common
attributes and behaviors for all nodes
With the exception of the first node in the scene graph—the root node—each
node in the scene graph has one parent.
Nodes with children are typically layout containers that arrange their child nodes
in the scene.
public class HelloWorld extends Application
{
HBox root = new HBox(); // all the nodes are set in a single horizontal row
root.getChildren().add(b1); //Retrieving the observable list of the Hbox Pane
primaryStage.setScene(sc);
primaryStage.show();
}
}
Stage Methods
show()
setScene()
setTitle(“title”)
getTitle()
setWidth(value)
setHeight(value)
getWidth()
getHeight()
setFullScreen(true)
isFullScreen()
close()
Example:
primaryStage.setTitle("Hello World");
primaryStage.show()
Layout / (Root nodes type) | Package javafx.scene.layout
HBox
VBox()
StackPane()
FlowPane()
BorderPane()
Adding nodes to the layout uses the add() or addAll() methods of the parent
Pane:
Hbox Layout
HBox lays out its children in a single horizontal row from left to right.
VBox Layout
VBox lays out its children in a single vertical column from top to bottom.
StackPane Layout
The node added first is placed at the bottom of the stack and the
next node is placed on top of it.
Sphere(double r): creates a new sphere with a
given radius.
Insets class stores the inside offsets for the four
sides of the rectangular area.
FlowPane Layout
FlowPane lays out nodes in rows or columns based on the available
horizontal or vertical space available.
It wraps nodes to the next line when the horizontal space is less
than the total of all the nodes' widths.
it wraps nodes to the next column when the vertical space is less
than the total of all the nodes' heights.
GridPane Layout
A GridPane arranges JavaFX components into columns and rows in a rectangular grid.
Each cell in a GridPane can be empty or can hold one or more JavaFX components,
including layout containers that arrange other controls.
Methods:
setTop(c)
setBottom(c)
setLeft()
setRight()
setCenter()
BorderPane Layout
JavaFX UI Controls
Label
Button
TextField
RadioButton
CheckBox
ListView
PasswordField
Menu
Table
Label
Constructors
Label()
Label(“new text”);
Label(“text”, ImageView)
Methods
setText(“new text”);
getText()
setTextFill(Color.RED)
setFont(new Font(“Times New Roman”,32));
setWrapText(true)
Button
Constructors
Button()
Button(“name”)
Methods
setText(“new text”)
getText()
setWrapText(true)
setDisable(true)
TextField
Constructors
TextField()
Methods
getText()
setText(”new text”);
setMaxWidth(12);
PasswordField
Constructor
PasswordField()
Methods
setMaxWidth(12)
RadioButton
rb1.setToggleGroup(gp)
rb2.setToggleGroup(gp)
Method
isSelected()
CheckBox
Method:
getValue()
EventHandling
When the user interacts with a control, the control generates an event.
Event Handling is the mechanism that controls the event and decides what
should happen, if an event occurs.
This mechanism has the code which is known as an event handler that is
executed when an event occurs.
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event)
{
l1.setText(“you clicked the button”);
} });
primaryStage.setScene(sc);
primaryStage.show();
}
EventHandling
Example2:
public void start(Stage primaryStage) throws Exception
{
TextField tf1 = new TextField()
tf1.setMaxWidth(20);
Button b1= new Button(“click here);
Label l1 = new Label()
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event)
{
l1.setText(“hello”+tf1.getText());
} });
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event)
{
if(rb1.isSelected())
l1.setText(“you selected”+rb11.getText());
} });
}
EventHandling
Example4:
public void start(Stage primaryStage) throws Exception
{
CheckBox cb1 = new CheckBox(“IT”);
CheckBox cb2 = new CheckBox(“CCE”);
Button b1= new Button(“click here);
Label l1 = new Label()
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event)
{
String bag;
if(cb1.isSelected())
bag = bag + cb1.getText()
if(cb1.isSelected())
bag = bag + cb1.getText()
if(cb1.isEmpty())
l1.setText(“you have not selected any items);
else
l1.setText(“selected items are”+bag);
} });
}
EventHandling
Example5:
public void start(Stage primaryStage) throws Exception
{
ComboBox<String> cb = new ComboBox<String>();
cb.getItems().add(“IT”);
cb.getItems().add(“CCE”);
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event)
{
if(cb1.getValue() == null)
l1.setText(“you have not selected any items);
else
l1.setText(“selected items are”+cb.getValue());
} });
}
MouseHandler
Example5:
public void start(Stage primaryStage) throws Exception
{
b1.setOnAction(new EventHandler<Event>() {
public void handle(Event event)
{
l2.setText(“you clicked the label”);
l2.setTextFill(Color.RED);
} });
FXML is a file format which JavaFX uses to create the layout of screens
The sole objective of this markup language is to specify a user interface (UI).
FXML code is separate from the program logic that’s defined in Java source
code.
This separation of the interface (the GUI) from the implementation (Java code)
makes it easier to debug, modify and maintain JavaFX GUI apps.
Class FXMLLoader’s static method load uses the FXML file that
represents the app’s GUI to creates the GUI’s scene graph and returns
a Parent (package javafx.scene) reference to the scene graph’s root
node.
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<HBox>
<Button>OK</Button>
<Button>CANCEL</Button>
<Button>CLEAR</Button>
</HBox>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox>
<Button>OK</Button>
<Button>CANCEL</Button>
<Button>CLEAR</Button>
</VBox>
Above file creates one button with id b1and one label with id l1.
On clicking the button method myButtonClick of MyController class is invoked.
<?xml version="1.0" encoding="UTF-8"?>
This line is an XML declaration, specifying the version of XML and the character
encoding used in the file. UTF-8 (Unicode Transformation Format) is an encoding system for
Unicode. It can translate any Unicode character to a matching unique binary string
Root Element: Hbox
@FXML
private Button b1;
@FXML
private Label l1;
@FXML
void myButtonClick(ActionEvent event) {
l1.setText("Button clicked");
}
}
The controller class uses a @FXML annotation on some members.
By using a @FXML annotation on a member, you are declaring that the FXML loader can
access the member even if it is private.
A public member used by the FXML loader does not need to be annotated with @FXML.
It is better to annotate all members, public and private, used by the FXML loader with the
@FXML annotation.
This tells the reader of your code how the members are being used.
Scene Builder Software
Scene Builder generates FXML, an XML-based markup language that enables
users to define an application's user interface, separately from the application
logic.
Scene Builder generates FXML, that records the widgets in the GUI, their
visible attributes and their relationship to each other.
A Controller class that must be completed by the programmer to bring the GUI
to life
SceneBuilder
SceneBuilder
END