0% found this document useful (0 votes)
2 views98 pages

Chapter 4 GUI Using Java FX

Chapter 4 discusses the transition from AWT and Swing to JavaFX for developing graphical user interfaces in Java. JavaFX offers a modern framework with features like multitouch support, built-in animation, and the ability to deploy on mobile devices. The chapter also covers the basic structure of JavaFX applications, including nodes, panes, and event-driven programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views98 pages

Chapter 4 GUI Using Java FX

Chapter 4 discusses the transition from AWT and Swing to JavaFX for developing graphical user interfaces in Java. JavaFX offers a modern framework with features like multitouch support, built-in animation, and the ability to deploy on mobile devices. The chapter also covers the basic structure of JavaFX applications, including nodes, panes, and event-driven programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 4

Graphical User Interfaces using Java FX

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 1
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.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 2
JavaFX vs Swing and AWT
❑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.
❑ Swing is designed for developing desktop GUI
applications
❑ With the release of Java 8, Swing is replaced by a
completely new GUI platform known as JavaFX.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 3
JavaFX
❑ JavaFX is a new framework for developing Java GUI
programs. It is an excellent example of how the object-
oriented principle is applied.
❑JavaFX incorporates modern GUI technologies to enable
you to develop rich GUI applications.
❑In addition, JavaFX provides a multitouch support for
touch-enabled devices such as tablets and smart phones.
❑JavaFX has a built-in 2D, 3D, animation support, and
video and audio playback.
❑Using third-party software, you can develop JavaFX
programs to be deployed on devices running iOS or
Android.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 4
Basic Structure of JavaFX
▪ Application

▪ Override the start(Stage) method


▪ Stage, Scene, and Nodes
Stage
Scene

Button

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 5
Stage

Scene

Button

by the JVM when the application is launched.

from resizing the stage, invoke [Link](false)


Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 6
window no matter how you resize it

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 7
Terms
❑ A node is a visual component such as a shape,
an image view, a UI control, a group, or a pane.
❑A shape refers to a text, line, circle, ellipse,
rectangle, arc, polygon, polyline, and so on.
❑A UI control refers to a label, button, check
box, radio button, text field, text area, and so
on.
❑A group is a container that groups a collection
of nodes.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 8
Panes, UI Controls, and Shapes

controls, and shapes


are subtypes of Node.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 9
Relationship Among Stage, Scene,
Node, Control, Group, and Pane

The relationship is illustrated in the UML diagram


shown in the previous slide.
❑Note a Scene can contain a Control, Group, or a Pane,
but not a Shape or an ImageView contained in a node.
❑A Pane or a Group can contain any subtype of Node.
❑You can create a Scene using the constructor
Scene(Parent, width, height) or Scene(Parent).

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 10
Display a Shape
This example displays a circle in the center of the pane.

x
Y Axis
(0, 0) X Axis

y
(x, y)
(0, 0)
X Axis
Java Conventional
Coordinate Coordinate
System
Y Axis System

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 11
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 12
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.
❑ A target listens to the changes in the source and
automatically updates itself once a change is made in the
source.
❑ A target binds with a source using the bind method
as follows:
[Link](source);
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 13
Binding Property:
getter, setter, and property getter

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 14
Uni/Bidirectional Binding
❑ The bind method is defined in the
[Link] interface.
❑ A binding property is an instance of
[Link].
❑ An observable source object is an instance of the
[Link] interface.
❑ An ObservableValue is an entity that wraps a value and
allows to observe the value for changes.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 15
❑ The syntax for setting a style is styleName:value.
❑ Multiple style properties for a node can be set together separated
by semicolon (;).
a style property is defined with a prefix -fx-

❑ For example, the following statement:


[Link]("−fx−stroke: black; −fx−fill: red;");
sets two JavaFX CSS properties for a circle.
❑ The above statement is equivalent to the following two
statements:
[Link]([Link]);
[Link]([Link]);

❑ If an incorrect JavaFX CSS is used, your program will still


compile and run, but the style will be ignored.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 16
The Color Class

range from 0.0 (completely transparent) to 1.0 (completely opaque)


Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 17
The Font Class

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 18
The Image Class

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 19
The ImageView Class

is a node for displaying an image.


Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 20
it is shared by three ImageView.
However, a node such as ImageView cannot be shared. You cannot
place an ImageView multiple times into a pane or scene.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 21
Layout Panes
❑Panes and groups are the containers for holding nodes.
❑The Group class is often used to group nodes and to
perform transformation and scale as a group.
❑Panes and UI control objects are resizable, but group,
shape, and text objects are not resizable.
❑ A Pane is usually used as a canvas for displaying shapes.
❑Pane is the base class for all specialized panes.
❑Each pane contains a list for holding nodes in the pane.
❑This list is an instance of ObservableList, which can be
obtained using pane’s getChildren() method.
❑You can use add(node) to add an element to the list and
addAll(node1, node2, ...) to add a variable number of
nodes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 22
Layout Panes
❑ JavaFX provides many types of panes for organizing
nodes in a container.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 23
FlowPane

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 24
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 25
GridPane

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 26
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 27
BorderPane

top, bottom, left, right, and center, using the


setTop(node), setBottom(node), setLeft(node),
setRight(node), and setCenter(node) methods
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 28
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 29
HBox

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 30
VBox

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 31
Shapes
JavaFX provides many shape classes for drawing texts,
lines, circles, rectangles, ellipses, arcs, polygons, and
polylines.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 32
Text

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 33
Text Example

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 34
Line

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 35
Rectangle

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 36
Rectangle Example

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 37
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 38
Circle

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 39
Ellipse

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 40
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 41
Arc

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 42
Arc Examples

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 43
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 44
-30, -20);
new Arc(x, y, radiusX, radiusY, -50, 20);

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 45
Polygon and Polyline
❑ The Polygon class defines a polygon that connects a sequence of
points,
❑ The Polyline class is similar to the Polygon class except that the
Polyline class is not automatically closed

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 46
Polygon
The getter and setter methods for property values and a getter for property
[Link] itself are provided in the class, but omitted in the UML diagram for brevity.

+Polygon() Creates an empty polygon.


+Polygon(double... points) Creates a polygon with the given points.
+getPoints(): Returns a list of double values as x- and y-coordinates of the points.
ObservableList<Double>

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 47
Event Handling
Suppose you want to write a GUI
program that lets the user enter a
loan amount, annual interest rate,
and number of years and click the
Compute Payment button to obtain
the monthly payment and total
payment. How do you accomplish
the task? You have to use event-
driven programming to write the
code to respond to the button-
clicking event.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 48
Procedural vs. Event-Driven
Programming
▪ Procedural programming is executed in procedural
order.
▪ are sequences of instructions to be executed.

▪In event-driven programming, code is executed


upon activation of events.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 49
Taste of Event-Driven Programming

The example displays a button in the frame. A


message is displayed on the console when a
button is clicked.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 50
Handling GUI Events
Source object (e.g., button)
Listener object contains a method for
processing the event.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 51
HBox pane = new HBox(); //H can be replaced by V
[Link](Pos.CENTER_LEFT);
Button btOK = new Button("OK");

OKHandlerClass1 handler1 = new OKHandlerClass1();


[Link](handler1);

[Link]().add(btOK);// addAll(btOk)

class OKHandlerClass1 implements


EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent a) {
[Link]("OK button clicked");
}
public void start(Stage primaryStage) {
……
// Create a scene and place it in the stage
Scene scene = new Scene(pane);

// Set the stage title


[Link]("HandleEvent");

// Place the scene in the stage


[Link](scene);
// Display the stage
[Link]();
Events
❑ An event can be defined as a type of signal
to the program that something has
happened.

❑ The event is generated by external user


actions such as mouse movements, mouse
clicks, or keystrokes.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 52
Event Classes

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 53
Event Information
An event object contains whatever properties are
related to the event.
You can identify the source object(button,txtbox)
of the event using the getSource() instance method
in the EventObject class.

The subclasses of EventObject deal with special


types of events, such as button actions, window
events, component events, mouse movements, and
keystrokes.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 54
Selected User Actions and Handlers

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 55
Inner Class Listeners
A listener class is designed specifically to
create a listener object for a GUI
component (e.g., a button).
It will not be shared by other applications.
So, it is appropriate to define the listener
class inside the frame class as an inner
class.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 56
Inner Classes
Inner class: A class is a member of another class.
Advantages: In some applications, you can use an inner
class to make programs simple.
An inner class can reference the data and methods
defined in the outer class in which it nests, so you do
not need to pass the reference of the outer class to the
constructor of the inner class.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 57
Inner Classes, cont.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 58
Inner Classes (cont.)
Inner classes can make programs simple and
concise.
An inner class supports the work of its
containing outer class and is compiled into a
class named
OuterClassName$[Link].
For example, the inner class InnerClass in
OuterClass is compiled into
OuterClass$[Link].
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 59
Inner Classes (cont.)
❑ An inner class can be declared public, protected,
or private subject to the same visibility rules
applied to a member of the class.
❑ An inner class can be declared static. A static
inner class can be accessed using the outer class
name.
❑ A static
inner class cannot access nonstatic
members of the outer class

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 60
Anonymous Inner Classes
❑ An anonymous inner class must always extend a superclass or
implement an interface, but it cannot have an explicit extends or
implements clause.
❑ An anonymous inner class must implement all the abstract
methods in the superclass or in the interface.
❑ An anonymous inner class always uses the no-arg constructor
from its superclass to create an instance. If an anonymous inner
class implements an interface, the constructor is Object().
❑ An anonymous inner class is compiled into a class named
OuterClassName$[Link]. For example, if the outer class Test
has two anonymous inner classes, these two classes are
compiled into Test$[Link] and Test$[Link].

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 61
Anonymous Inner Classes (cont.)
Inner class listeners can be shortened using anonymous
inner classes.
An anonymous inner class is an inner class without a
name. It combines declaring an inner class and creating an
instance of the class in one step.
An anonymous inner class is declared as follows:
new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 62
Anonymous Inner Classes (cont.)

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 63
Simplifying Event Handing Using
Lambda Expressions
Lambda expression is a new feature in Java 8. Lambda
expressions can be viewed as an anonymous method with a
concise syntax. For example, the following code in (a) can
be greatly simplified using a lambda expression in (b) in
three lines.
[Link]( [Link](e -> {
new EventHandler<ActionEvent>() { // Code for processing event e
@Override });
public void handle(ActionEvent e) {
// Code for processing event e
}
}
});

(a) Anonymous inner class event handler (b) Lambda expression event handler

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 64
Basic Syntax for a Lambda Expression
The basic syntax for a lambda expression is either
(type1 param1, type2 param2, ...) -> expression
or
(type1 param1, type2 param2, ...) -> { statements; }

The data type for a parameter may be explicitly


declared or implicitly inferred by the compiler. The
parentheses can be omitted if there is only one
parameter without an explicit data type.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 65
Single Abstract Method Interface (SAM)
The statements in the lambda expression is all for
that method. If it contains multiple methods, the
compiler will not be able to compile the lambda
expression. So, for the compiler to understand
lambda expressions, the interface must contain
exactly one abstract method. Such an interface is
known as a functional interface, or a Single Abstract
Method (SAM) interface.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 66
MouseEvent

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 67
The KeyEvent Class

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 68
The KeyCode Constants

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 69
User Interface Controls
A graphical user interface (GUI) makes a system
user-friendly and easy to use.
Creating a GUI requires creativity and knowledge
of how GUI components work.
Since the GUI components in Java are very
flexible and versatile, you can create a wide
assortment of useful user interfaces.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
70
Frequently Used UI Controls

The prefixes lbl, bt, chk, rb, tf, pf, ta, cbo, lv, scb, sld, and mp are
used to name reference variables for Label, Button, CheckBox,
RadioButton, TextField, PasswordField, TextArea, ComboBox,
ListView, ScrollBar, Slider, and MediaPlayer.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
71
Labeled
A label is a display area for a short text, a node, or both. It is often
used to label other controls (usually text fields). Labels and buttons
share many common properties. These common properties are
defined in the Labeled class.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
72
Label
The Label class defines labels.

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
73
ButtonBase and Button
A button is a control that triggers an action event when clicked.
JavaFX provides regular buttons, toggle buttons, check box
buttons, and radio buttons. The common features of these buttons
are defined in ButtonBase and Labeled classes.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
74
Button Example

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
75
CheckBox
A CheckBox is used for the user to make a selection. Like Button,
CheckBox inherits all the properties such as onAction, text,
graphic, alignment, graphicTextGap, textFill, contentDisplay
from ButtonBase and Labeled.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
76
CheckBox Example

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
77
RadioButton
Radio buttons, also known as option buttons, enable you to
choose a single item from a group of choices. In appearance
radio buttons resemble check boxes, but check boxes display a
square that is either checked or blank, whereas radio buttons
display a circle that is either filled (if selected) or blank (if not

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
78
RadioButton Example

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
79
TextField
A text field can be used to enter or display a string. TextField is a
subclass of TextInputControl.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
80
TextField Example

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
81
TextArea
A TextArea enables the user to enter multiple lines of text.

m
m

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
82
E
TextArea Example

Run
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
83
ComboBox
A combo box, also known as a choice list or drop-down list,
contains a list of items from which the user can choose.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
84
ComboBox Example

FThis example lets users view an image and a


description of a country's flag by selecting the
country from a combo box.

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
85
ListView
A list view is a component that performs basically the same function as a
combo box, but it enables the user to choose a single value or multiple
values.

m
m

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
86
E
Example: Using ListView
This example gives
a program that lets
users select
countries in a list
and display the flags
of the selected
countries in the
labels.

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
87
ScrollBar
A scroll bar is a control that enables the user to select from a range of values. The
scrollbar appears in two styles: horizontal and vertical.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
88
Scroll Bar Properties

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
89
Example: Using Scrollbars
This example uses
horizontal and vertical
scrollbars to control a
message displayed on a
panel. The horizontal
scrollbar is used to move
the message to the left or
the right, and the vertical
scrollbar to move it up and
down.
Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
90
Slider
Slider is similar to
ScrollBar, but Slider has
more properties and can
appear in many forms.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
91
Example: Using Sliders
Rewrite the preceding
program using the sliders
to control a message
displayed on a panel
instead of using scroll
bars.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
92
Media
You can use the Media class to obtain the source of the media, the MediaPlayer
class to play and control the media, and the MediaView class to display the video.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
93
MediaPlayer
The MediaPlayer class playes and controls the media with properties such as
autoPlay, currentCount, cycleCount, mute, volume, and totalDuration.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
94
MediaView
The MediaView class is a subclass of Node that provides a view of the Media
being played by a MediaPlayer. The MediaView class provides the properties for
viewing the media.

m
m

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
95
E
Example: Using Media
This example displays a
video in a view. You can use
the play/pause button to
play or pause the video and
use the rewind button to
restart the video, and use the
slider to control the volume
of the audio.

Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
96

You might also like