GUI Using JavaFX Components
GUI Using JavaFX Components
GUI Programming-
Java
Faculty Of Computing,
Bahir Dar Institute of Technology,
Bahir Dar University
Objectives
To write a simple JavaFX program and understand the
relationship among stages, scenes, and nodes.
To create user interfaces using panes, UI controls, and
shapes.
To use the common properties style and rotate for
nodes.
To create colors using the Color class.
To create fonts using the Font class.
To create images using the Image class and to create
image views using the ImageView class.
To layout nodes using Pane, StackPane, FlowPane,
GridPane, BorderPane, HBox, and VBox .
To display text using the Text class and create
shapes using Line, Circle, Rectangle, Ellipse, Arc,
Creating GUI Objects
// Create a button with text OK
Button btOK = new Button("OK");
Button
Combo
// Create a text field with text "Type Name Here"
TextField tfName = new TextField("Type Name Here");
Box
GridPane ScrollPane
Pane
AnchorPane SplitePane
StackPane
TilePane Tab
BorderPane
TextFlow TitledPane
HBox
Accordion ToolBar
VBox
ButtonBar
FlowPane
DialogPane
JavaFX Layout Managers
JavaFX provides many types of panes for automatically laying
out nodes in a desired location and size.
Class Description
Base class for layout panes. It contains the getChildren()
Pane
method for returning a list of nodes in the pane.
Places the nodes on top of each other in the center of the
StackPane
pane.
FlowPane Places the nodes row-by-row horizontally .
GridPane Places the nodes in the cells in a two-dimensional grid.
Places the nodes in the top, right, bottom, left, and center
BorderPane
regions.
HBox Places the nodes in a single row.
VBox Places the nodes in a single column.
Steps to create layout: In order to create the layouts, we need to follow the
following steps.
Instantiate the respective layout class, for example, HBox root = new HBox();
Setting the properties for the layout, for example, root.setSpacing(20);
Adding nodes to the layout object, for example,
root.getChildren().addAll(<NodeObjects>);
JavaFX Layout Managers
FlowPane GridPane
Example:
Button btn = new Button("OK");
btn.setTextFill(Color.RED);
The Font Class
A Font describes font name, weight, and size.
You can set fonts for rendering the text.
The javafx.scene.text.Font class is used to
create fonts.
A Font is defined by its name, weight,
posture, and size.
The font postures are two constants:
FontPosture.ITALIC and
FontPosture.REGULAR.
Font font1 = new Font("SansSerif", 16);
Font font2 = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.ITALIC, 12);
The Image and ImageView Classes
• The Image class represents a graphical image and the
ImageView class can be used to display an image.
• The javafx.scene.image.Image class is used for
loading an image from a specified filename or a URL.
• The javafx.scene.image.ImageView is a node for
displaying an image.
• An ImageView can be created from an Image object.
• For example:
Image image = new Image("image/us.gif");
ImageView imageView = new ImageView(image);
• Alternatively, you can create an ImageView directly
from a file or a URL as follows:
ImageView imageView = new ImageView("image/us.gif");
JavaFX UI Controls
The package javafx.scene.control provides all the
necessary classes for the UI components .
Label TextField ContextMenu
Button PasswordField Separator