0% found this document useful (0 votes)
42 views

14 JavaFX Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

14 JavaFX Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 14

JavaFX Basics

1
Motivations
JavaFX is a new framework for developing Java GUI
programs. The JavaFX API is an excellent example
of how the object-oriented principle is applied.
This chapter serves two purposes. First, it presents
the basics of JavaFX programming. Second, it uses
JavaFX to demonstrate OOP. Specifically, this
chapter introduces the framework of JavaFX and
discusses JavaFX GUI components and their
relationships.

2
Objectives
• To distinguish between JavaFX, Swing, and AWT (§14.2).
• To write a simple JavaFX program and understand the relationship among stages,
scenes, and nodes (§14.3).
• To create user interfaces using panes, UI controls, and shapes (§14.4).
• To use binding properties to synchronize property values (§14.5).
• To use the common properties style and rotate for nodes (§14.6).
• To create colors using the Color class (§14.7).
• To create fonts using the Font class (§14.8).
• To create images using the Image class and to create image views using the
ImageView class (§14.9).
• To layout nodes using Pane, StackPane, FlowPane, GridPane, BorderPane, HBox, and
VBox (§14.10).
• To display text using the Text class and create shapes using Line, Circle, Rectangle,
Ellipse, Arc, Polygon, and Polyline (§14.11).
• To develop the reusable GUI components ClockPane for displaying an analog clock
(§14.12).

3
JavaFX vs Swing and AWT
Swing and AWT are replaced by the JavaFX platform for
developing rich Internet applications.

When Java was introduced, the GUI classes were bundled in a


library known as the Abstract Windows Toolkit (AWT). AWT is fine
for developing simple graphical user interfaces, but not for
developing comprehensive GUI projects. In addition, AWT is
prone to platform-specific bugs. The AWT user-interface
components were replaced by a more robust, versatile, and
flexible library known as Swing components. Swing components
are painted directly on canvases using Java code. Swing
components depend less on the target platform and use less of
the native GUI resource. With the release of Java 8, Swing is
replaced by a completely new GUI platform known as JavaFX.

4
Basic Structure of JavaFX
• Application
• Override the start(Stage) method
• Stage, Scene, and Nodes

MyJavaFX Run

MultipleStageDem Run
o
5
Panes, UI Controls, and Shapes

ButtonInPane Run
6
Display a Shape
This example displays a circle in the center of the pane.

ShowCircle Run

7
Binding Properties
JavaFX introduces a new concept called binding property
that enables a target object to be bound to a source
object. If the value in the source object changes, the
target property is also changed automatically. The target
object is simply called a binding object or a binding
property.

ShowCircleCentere Run
d
8
Binding Property:
getter, setter, and property getter

9
Uni/Bidirectional Binding

BidirctionalBindingDemo Run

10
Common Properties and Methods
for Nodes
• style: set a JavaFX CSS style

• rotate: Rotate a node

NodeStyleRotateDemo Run

11
The Color Class

12
The Font Class

FontDemo Run
13
The Image Class

14
The ImageView Class

ShowImage Run
15
Layout Panes
JavaFX provides many types of panes for organizing nodes
in a container.

16
FlowPane

ShowFlowPan Run
e 17
GridPane

ShowGridPan
e
Run

18
BorderPane

ShowBorderPa Run
ne 19
HBox

20
VBox

ShowHBoxVBo Run
x 21
Shapes
JavaFX provides many shape classes for drawing texts,
lines, circles, rectangles, ellipses, arcs, polygons, and
polylines.

22
Text

23
Text Example

ShowText Run
24
Line

ShowLine

Run
25
Rectangle

26
Rectangle Example

ShowRectangle Run
27
Circle

28
Ellipse

ShowEllipse

Run
29
Arc

30
Arc Examples

ShowArc Run
31
Polygon and Polyline

ShowArc Run
32
Polygon

ShowPolygon Run

33
Case Study: The ClockPane Class
This case study develops a class that displays a clock on a
pane.

ClockPane

34
Use the ClockPane Class

DisplayClock Run

35

You might also like