Mettu University: Faculty of Engineering & Technology Department of Computer Science
Mettu University: Faculty of Engineering & Technology Department of Computer Science
METTU UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
Java programming Course
Chapter-3
Java GUI using JAVAFX
By: Naol G. 2
Introduction
• JavaFX is a GUI toolkit for Java Graphical User Interface.
• JavaFX is a Java library used to build Rich Internet Applications.
• Rich Internet Applications are those web applications which provide similar
features and experience as that of desktop applications.
• The applications written using JavaFX library can run consistently
across multiple platforms.
• Windows, Linux, Mac, Android…
• The applications developed using JavaFX can run on various devices
such as Desktop Computers, Mobile Phones, TVs, Tablets, etc..
By: Naol G. 3
JavaFX Features
• JavaFX
• comes with a large set of built-in GUI components, like buttons, text fields,
tables, trees, menus, charts and much more.
• can be styled via CSS and / or programmatically.
• comes with a built-in chart library you can use for simple charts.
• has support for 2D and 3D Graphics.
• has a WebView which can display modern web applications.
By: Naol G. 4
JavaFX Structure
• It is useful to understand how JavaFX is designed and to have a good
overview of what features it contains.
• A JavaFX application contains one or more stages which corresponds to windows.
• Each stage has a scene attached to it.
• Each scene can have an object graph of controls, layouts etc. attached to it, called
the scene graph.
By: Naol G. 5
• Stage
• The stage is the outer frame for a JavaFX application.
• The stage typically corresponds to a window.
• A JavaFX application has a primary Stage object which is created for you
by the JavaFX runtime.
• Scene
• To display anything on a stage in a JavaFX application, you need a scene.
• A stage can only show one scene at a time, but it is possible to exchange
the scene at runtime.
• A scene is represented by a Scene object inside a JavaFX application.
By: Naol G. 7
• Scene Graph
• All visual components (controls, layouts etc.) must be attached to a scene
to be displayed, and that scene must be attached to a stage for the whole
scene to be visible.
• The total object graph of all the controls, layouts etc. attached to a scene
is called the scene graph.
• Nodes
• All components attached to the scene graph are called nodes. All nodes
are subclasses of a JavaFX class called javafx.scene.Node .
• There are two types of nodes: Branch nodes and leaf nodes.
• A branch node: that can contain other nodes (child nodes). Branch nodes are
also referred to as parent nodes because they can contain child nodes.
• A leaf node: a node which cannot contain other nodes.
By: Naol G. 8
• Controls
• JavaFX controls are JavaFX components which provide some kind of control
functionality inside a JavaFX application.
• For instance, a button, radio button, table, tree etc.
• For a control to be visible it must be attached to the scene graph of some
Scene object.
• Layouts
• JavaFX layouts are components that contains other components inside them.
• JavaFX layout components are also called parent components because they
contain child components.
• JavaFX layout components are subclasses of the class javafx.scene.Parent.
• Example: Hbox, Vbox, FlowPane, StackPane, TilePane, GridPane, AnchorPane
By: Naol G. 9
• Implementing start()
• All subclasses of the JavaFX Application
class must implement the abstract start()
method of the Application class (or be an
abstract subclass of Application itself).
• The start() method is called when the
JavaFX application is started.
• It takes a single parameter of the type Stage
• The Stage object is created for you by the
JavaFX runtime.
• The example above sets a title on the stage
object and then calls show() on it.
• Here is the example from above, but with the
start() method implemented:
By: Naol G. 11
• Adding a Scene
• The previous JavaFX examples only open a
window, but nothing is displayed inside this
window.
• To display something inside the JavaFX
application window you must add a Scene to
the Stage object.
• This is done inside the start() method.
• All components to be displayed inside a
JavaFX application must be located inside a
scene.
• The names for "stage" and "scene" are
inspired by a theater.
• A stage can display multiple scenes, just like
in a theater play.
• Here is an example of how to add a Scene
object to the Stage along with a simple Label:
By: Naol G. 13
JavaFX Stage
• Creating a Stage
• Showing a Stage
• show() vs. showAndWait()
• Set a Scene on a Stage
• Stage Title
• Stage Position
• Stage Width and Height
• Stage Full Screen Mode
• Stage Life Cycle Events
• Close Stage Event Listener
• Hiding Stage Event Listener
• Hidden Stage Event Listener
• Showing Stage Event Listener
• Shown Stage Event Listener
By: Naol G. 15
• Creating a Stage
• You create a JavaFX Stage object just like any other Java object:
• Using the new command and the Stage constructor.
• Here is an example of creating a JavaFX Stage object.
• Showing a Stage
• Simple creating a JavaFX Stage object will not show it.
• In order to make the Stage visible you must call either its show() or
showAndWait() method.
• Here is an example of showing a JavaFX Stage:
By: Naol G. 16
• Stage Title
• You can set the JavaFX Stage title via the Stage setTitle() method.
• The Stage title is displayed in the title bar of the Stage window.
• Here is an example of setting the title of a JavaFX Stage:
• Stage Position
• You can set the position (X,Y) of a JavaFX Stage via its setX() and setY()
methods.
• Here is an example of setting the X and Y position of a JavaFX Stage object:
Note:
• It might be necessary to also set the width
and height of the Stage if you set the X and
Y position, or the stage window might
become very small.
By: Naol G. 18
JAVAFX SCENE
Create Scene
Set Scene on Stage
By: Naol G. 22
• Create Scene
• You create a JavaFX Scene object via its
constructor.
• As parameter you must pass the root JavaFX
component to be displayed inside the Scene.
• Here is an example:
A JavaFX Scene can be attached to only single Stage at a time, and Stage can also only display
one Scene at a time
By: Naol G. 23
JavaFX Layouts
• In JavaFX, Layout defines the way in which the components are to be seen
on the stage.
• It basically organizes the scene-graph nodes.
• We have several built-in layout panes in JavaFX that are:
• HBox, VBox, StackPane, FlowBox, AnchorPane, etc.
• VBox
• VBox helps in organizing the node in a
vertical column.
• In this, the content area’s default height
can display the children in its preferred
height and default width is the greatest of
the children’s width.
• Even though the locations cannot be set
for the children since it is automatically
computed, it can be controlled to an
extent by customization of VBox
properties.
By: Naol G. 25
• HBox
• HBox works in the opposite
concept of VBox.
• That is, nodes will be organized
horizontally.
• Following is a program that helps
in understanding HBox.
By: Naol G. 26
• BorderPane
• In this, layout structure has five
regions such as TOP, BOTTOM,
CENTRE, LEFT, and RIGHT.
By: Naol G. 27
• FlowPane
• FlowPane permits the user to layout
the nodes in a consecutive manner
and wraps the nodes at the boundary.
• Here, the nodes can be in the vertical
direction (columns) or horizontal
direction (rows).
By: Naol G. 28
• GridPane
• GridPane Layout pane allows us to add the multiple nodes in multiple rows
and columns.
• It is seen as a flexible grid of rows and columns where nodes can be
placed in any cell of the grid.
• It is represented by javafx.scence.layout.GridPane class.
By: Naol G. 29
JAVAFX FONTS
Create Font Instance
Using the Font Instance
List Installed Font Families and Names
By: Naol G. 30
The JavaFX Font class, java.scene.text.Font, enables you to load different Fonts for use in your JavaFX
applications. A font is a text style. All text rendered with the same font will look similar.
As you can see, the Font factory methods enable you to create Font instances representing different
font families, font sizes, font weights and font postures.
By: Naol G. 31
JavaFX UI Controls
JAVAFX LABEL
Creating a Label
Changing the Text of a Label
Set Label Font
By: Naol G. 32
The JavaFX Label control can display a text/image label inside JavaFX GUI. It must be added to
the scene graph to be visible. It is represented by the class javafx.scene.control.Label.
• Creating a Label
• You create a label control instance by creating an instance of Label class.
• Here is a JavaFX Label instantiation example:
This example tells the Label to use the Arial font with a size of 24.
By: Naol G. 33
JAVAFX BUTTON
Creating a Button
Button Text
Button Font
Button Size
Button Events
By: Naol G. 34
A JavaFX Button control enables a JavaFX application to have some action executed when the
application user clicks the button.It is represented by class javafx.scene.control.Button
It can have a text and an icon on it which indicate to the user what clicking the button will do.
• Creating a Button
• You create a button control by creating an instance of the Button class.
• Here is a JavaFX Button instantiation example:
• Button Text
• There are two ways to set the text of a JavaFX button.
• The first way is to pass the text to the Button constructor.
• The second way to set the button text is by calling the setText() method on the
Button instance.
• Here is an example how how calling setText() on a JavaFX Button looks:
• Button Font
• You can specify what font the text on a JavaFX Button should be rendered with via
its setFont() method. Here is an example of setting a font on a JavaFX Button:
By: Naol G. 36
• Button Size
• The JavaFX Button class contains a set of methods you can use to set the
button size.
• The methods controlling the button size are:
By: Naol G. 37
• The methods setMinSize(), setMaxSize() and setPrefSize() sets both width and height
for the button in a single call.
• Thus, these methods takes both a width and a height parameter. For instance, calling
• is equivalent to calling
• Button Events
• In order to respond to the click of a button you need to attach an event listener to
the Button object. Here is how that looks:
• Here is how attaching a click event listener looks with a Java Lambda expression:
By: Naol G. 39
JAVAFX TEXTFIELD
Creating a TextField
Setting the Text of a TextField
By: Naol G. 41
A JavaFX TextField control enables users of a JavaFX application to enter text which can
then be read by the application. It is represented by the
class javafx.scene.control.TextField .
• Creating a TextField
• You create a TextField control by creating an instance of the TextField class.
• Here is a JavaFX TextField instantiation example:
JAVAFX PASSWORDFIELD
Creating a PasswordField
Adding a PasswordField to the Scene Graph
By: Naol G. 44
A JavaFX PasswordField control enables users of a JavaFX application to enter password which can then be
read by the application. The PasswordField control does not show the texted entered into it. Instead it shows a circle
for each character entered. It is represented by the class javafx.scene.control.PasswordField .
• Creating a PasswordField
• You create a PasswordField control by
creating an instance of the PasswordField
class.
• Here is a JavaFX PasswordField instantiation
example:
PasswordField passwordField = new PasswordField();
JAVAFX TEXTAREA
Creating a TextArea
Adding a TextArea to the Scene Graph
Reading the Text of a TextArea
Setting the Text of a TextArea
By: Naol G. 46
A JavaFX TextArea control enables users of a JavaFX application to enter text spanning multiple
lines, which can then be read by the application.
The JavaFX TextArea control is represented by the class javafx.scene.control.TextArea .
• Creating a TextArea
• You create a TextArea control by creating
an instance of the TextArea class. Here is
a JavaFX TextArea instantiation example:
Output
By: Naol G. 47
• RadioButton
• An instance of the RadioButton class represents a radio button.
• It inherits from the ToggleButton class.
• Therefore, it has all of the features of a toggle button.
• A radio button is rendered differently compared to a toggle button.
• Like a toggle button, a radio button can be in one of the two states: •
• Selected
• Unselected
By: Naol G. 49
JAVAFX CHECKBOX
Creating a CheckBox
Reading Selected State
By: Naol G. 51
A JavaFX CheckBox is a button which can be in three different states: Selected, not selected and
unknown (indeterminate). The JavaFX CheckBox control is represented by the
class javafx.scene.control.CheckBox.
• Creating a CheckBox
• You create a JavaFX CheckBox control via the CheckBox constructor. Here is a
JavaFX CheckBox instantiation example:
typeOnechbx.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov, final Boolean value, final Boolean newValue){
if(newValue != null && newValue){
label.setText(“You selected t your type CheckBox“);
}
}
});
By: Naol G. 54
JAVAFX COMBOBOX
Creating a ComboBox
Adding Choices to a ComboBox
Adding a ComboBox to the Scene Graph
By: Naol G. 55
The JavaFX ComboBox control enables users to choose an option from a predefined list of choices,
or type in another value if none of the predefined choices matches what the user want to select. The
JavaFX ComboBox control is represented by the class javafx.scene.control.ComboBox.
• Creating a ComboBox
• You create a ComboBox simply by creating a new instance of the ComboBox class.
• Here is a JavaFX ComboBox instantiation example:
Example:
By: Naol G. 58
JAVAFX MENUBAR
Creating a MenuBar Instance
Creating Menu Instances
Adding Menu Items
Submenus
Check Menu Items
Menu Item Separators
By: Naol G. 59
The JavaFX MenuBar provides JavaFX applications with a visual drop down menu similar to that most desktop
applications have at the top of their application window. The JavaFX MenuBar is represented by the
class javafx.scene.control.MenuBar
• Notice how the MenuBar is added to the root layout (e.g. VBox) of the JavaFX scene.
• This places the MenuBar at the top of the application window.
• Creating Menu Instances
• Once the MenuBar instance is created, you can add Menu instances to it.
• A Menu instance represents a single vertical menu with nested menu items.
• Thus, you can add multiple Menu instances to a MenuBar. Here is an example of adding
a Menu to a MenuBar :
Menu menu1 = new Menu("Menu 1");
menuBar.getMenus().add(menu1);
By: Naol G. 60
menu.getItems().add(menuItem1);
menu.getItems().add(menuItem2);
• Submenus
• The JavaFX MenuBar supports multiple layers of menus.
• A menu nested inside another menu is called a submenu.
• The Menu class extends the MenuItem class and can therefore be used as a menu item
inside another Menu instance.
• Here is an example that creates a single JavaFX menu with a submenu inside:
• You can set multiple CSS properties inside the same style string. Here is an
example of how that looks:
By: Naol G. 67
• Reading assignment:
• JavaFX Animations
• Translation
• Transformation
• Scaling
• Reference: http://tutorials.jenkov.com/javafx/index.html
Thank you!