GUI JavaFx
GUI JavaFx
When the interaction uses a screen and keyboard, it is called a Console Interface.
When the user interaction uses picture-like tools such as buttons and textboxes, it is called a Graphical User
Interface (GUI).
Many Java applications use a Graphical User Interface or GUI (pronounced “gooey”).
User Interface
(UI) Design the UI by using Scene Builder (a
drag-and-drop tool) which provides the
structure of the user interface by an
XML-based language called FAXML,
Logic Code separate from the application logic of the
code.
A window in a GUI commonly consists of several controls that present data to the user
and/or allow interaction with the application.
Some of the common GUI controls are buttons, labels, text fields, check boxes, and radio
buttons.
Labe TextFiel
l d
Button
Dr. Ali Alsaffar 4
Graphical User Interfaces (3 of 3)
Examples of events are clicking a
Programs that operate in a GUI environment must be button, moving the mouse over a
control, pressing a key on the
event-driven. keyboard, etc.
An event listener is a method that automatically Clicking the button by the mouse
creates an Event object that is passed
executes when a specific event occurs. to the listener, then the listener is
executed.
Dr. Ali Alsaffar 5
Introduction to JavaFX (1 of 5)
JavaFX is a Java library for developing rich applications that employ graphics.
The Label and the TextField are grouped in a horizontal box (HBox).
The HBox and the Button are grouped in a vertical box (VBox).
Label HBox
root
node
Scene VBox
HBox
Label
VBox Button
TextField
TextField
Button
Dr. Ali Alsaffar 9
Introduction to JavaFX (5 of 5)
The Application class has an abstract method named start, which is the entry point
for the application
Because the start method is abstract, you must override it
import javafx.application.Application;
Various import statements import javafx.stage.Stage;
import javafx.scene.Scene;
Other import statements…
A class that extends the
public class ClassName extends Application
Application class {
public static void main(String[] args)
{
A main method // Launch the application.
launch(args);
}
A start method
@Override
public void start(Stage primaryStage)
{
// Insert startup code here.
}
}
Empty Window
1. Import the class for the control from the necessary javafx package.
Example: import javafx.scene.control.Label;
You use layout containers to arrange the positions of controls on the screen.
JavaFX provides many layout containers.
We will start with these:
HBox: Arranges controls in a single horizontal row.
VBox: Arranges controls in a single vertical column.
GridPane: Arranges controls in a grid with rows and columns.
Example
// Create a Label control Instantiate the control (Label)
Label lblMessage = new Label("Hello World");
// Create an HBox container and add the label Instantiate the container (HBox)
HBox hbox = new HBox(lblMessage); and add the control (Label) to the
container.
// Create a Scene and add the HBox as the root node
Scene scene = new Scene(hbox); Instantiate the scene (Scene) and
add the container (HBox) to the
scene
Dr. Ali Alsaffar 17
Adding the Scene to the Stage
Once you've created a Scene object, you add it to the application's stage.
The stage is an instance of the Stage class from the javafx.stage package.
You do not have to instantiate the Stage class, however. It is created automatically and
passed as an argument to the start method.
@Override
public void start(Stage primaryStage)
{
...
primaryStage.setScene(scene)
}
@Override
public void start(Stage primaryStage)
{
Label lblMessage = new Label("Hello World"); Make a Label control
VBox vbox = new VBox(lblMessage); Create a container Vbox and put the label in it
Scene scene = new Scene(vbox); Make the VBox the root node in the scene
primaryStage.setScene(scene); Set the scene to the stage
primaryStage.show(); Show the stage (display it)
}
}
@Override
public void start(Stage primaryStage)
{
Label lblMessage = new Label("Hello World");
VBox vbox = new VBox(lblMessage);
vbox.setAlignment(Pos.CENTER); Center-align the VBox
Scene scene = new Scene(vbox, 300, 100);
primaryStage.setScene(scene);
primaryStage.setTitle("Hello");
primaryStage.show();
Width Height
}
}
Dr. Ali Alsaffar 20
Displaying Images
The ImageView class, also from the // Create a Scene with the HBox as its root node.
Scene scene = new Scene(hbox);
javafx.scene.image package // Add the Scene to the Stage.
primaryStage.setScene(scene);
// Set the stage title.
Use this class to create a node that primaryStage.setTitle("Hot Air Balloon");
// Show the window.
displays the image primaryStage.show();
}
Spacing
HBox space
VBox space
method.
Padding
You pass an Insets object as an argument to the 25
setPadding method. 20
hbox.setPadding(new Insets(10));
HBox
Spacing between controls
is 10
VBox
VBox
Spacing between controls
Spacing between controls
is 10
is 10
VBox
Spacing between controls
is 10
2. Second, you add controls to the container using the add method
gridPane.add(control, column, row);
gridpane.setVgap(10);
Rows Vgap
0, 1, 2, 3
Vgap
The GridPane container also has a setPadding method to set
Vgap
the padding around the container’s inside edge:
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(10));
Hgap
To get the particular screen layout that you desire, you will sometimes have to nest layout
containers.
Event objects are instances of the Event class (from the javafx.event package), or one
of its subclasses.
InputEvent javafx. Created when an event that results from user input, such as a mouse or key click,
scene.input occurs.
KeyEvent javafx. Created when the user presses a key on the keyboard. This event can be used to watch
scene.input for specific keystrokes entered by the user. (KeyEvent is a subclass of InputEvent.)
Created when the user does something interesting with the mouse,
MouseEvent javafx. such as clicking one of the buttons, dragging the mouse, or simply
scene.input moving the mouse cursor over another object. (MouseEvent is a
subclass of InputEvent.)
TouchEvent javafx. Created when a user initiates a touch event on a device that allows
scene.input touch input.
WindowEvent javafx.stage Created when the status of the window (stage) changes.
The process of connecting an event handler object to a control is called registering the event
handler.
Button controls have a method named setOnAction that registers an event handler:
mybutton.setOnAction(new ButtonClickHandler());
When the user clicks the button, the event handler object’s handle method will be executed.
A functional interface is an interface that has one, and only one, abstract method.
The EventHandler interface has only one abstract method is a functional interface.
Consider to use lambda expression to implement functional interface because it is more concise than the code
for instantiating an inner class.
public class KiloConverter extends Application{ public class KiloConverter extends Application {
@Override ...
public void start(Stage primaryStage) { @Override
... public void start(Stage primaryStage){
// Register an event handler. ...
myButton.setOnAction(new ButtonClickHandler()); // Register an event handler.
... myButton.setOnAction(e -> btn_Clicked(e));
} ...
}
class ButtonClickHandler implements
EventHandler<ActionEvent>{ private void btn_Clicked(ActionEvent e)
@Override {
public void handle(ActionEvent event) { myLabel.setText(“You clicked he button!");
myLabel.setText(“You clicked he button!"); }
} ...
}
...
Also, consider to use double column operator (::) to implement functional interface.
public class KiloConverter extends Application public class KiloConverter extends Application {
{ ...
... @Override
@Override public void start(Stage primaryStage){
public void start(Stage primaryStage){ ...
... // Register an event handler.
// Register an event handler. myButton.setOnAction(this::btn_Clicked);
myButton.setOnAction(e -> btn_Clicked(e)); ...
... }
}
private void btn_Clicked(ActionEvent e)
private void btn_Clicked(ActionEvent e) {
{ myLabel.setText(“You clicked he button!");
myLabel.setText(“You clicked he button!"); }
} ...
...
Write a JavaFx application that has two buttons and a label as shown in the below GUI.
230
75
Spacing 10
Padding
20
Label
Buttons
To retrieve the text that the user has typed into a TextField control, call the control’s
You getText
can supply method.
a default value, e.g.
The method returns the value that has been entered, as a String. “0.0”
TextField(“0.0”)
Example
String input;
input = myTextField.getText();
Write a JavaFx application that converts a value from kilometers into miles. Design the following
User Interface (UI) for your application:
TextField
Button
Label
When the user enters a value in the text field and clicks the Convert button, the application
displays the result using a label below the button.
Dr. Ali Alsaffar 41
Dr. Ali Alsaffar 42
RadioButton Controls (1 of 3)
RadioButton controls allow the user to select one choice from several possible options.
Write a JavaFx application that has two radio buttons and an image viewer.
When the user clicks Flower radio button, the image viewer displays a flower image.
When the user clicks the Sunset radio button, the image viewer displays a sunset image.
Once you have created a ComboBox control, you are ready to add items to it:
setEditable(value) The value argument is a boolean. If value is true, the ComboBox will be editable. If value is
false, the ComboBox will be uneditable.
You can use the ComboBox class’s getValue() method to get the item that is currently selected:
String selected = comboBox.getValue();
If no item is selected in the ComboBox, the method will return null.
To immediately perform an action when the user selects an item in a ComboBox, write an event
handler that responds to the ActionEvent:
Write a JavaFx application that converts a distance measure from kilometers to miles, and vice versa. Design
the below GUI for the application.
The conversion is computed instantly while typing the distance value or selecting a distance unit (Kilometer
or Mile) from the combobox control.
If the entered value is not numeric, change the input style to red over yellow and the error message to red as
an indication of invalid input.
Dr. Ali Alsaffar 54
The BorderPane Layout Container (1 of 2)
Summary of constructors:
Constructor Description
BorderPane() The no-arg constructor creates an empty
BorderPane container.
The BorderPane class provides the following methods to add controls to specific regions:
setCenter(), setTop(), setBottom(), setLeft(), setRight()