JavaFX Controls
JavaFX Controls
JavaFX UI Controls
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Frequently Used UI Controls
Throughout this chapter, 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, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
RadioButtons,
arranged in groups.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
S l i d e r s are similar to
ScrollBars (whichthis
UI doesn’t happen to
have, but which you are,
no doubt, already
familiar with for
scrolling the screen).
S l i d e r s differ from
ScrollBars, though,in
that a S l i d e r lets us
select a value from a
range, whereas a
Sc r o l l Ba r is usually
used to let us scroll
content that doesn’t fit
into its container
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
The ImageView can be
used to either provide
static information, or the
image it displays can
change depending on
what the user does while
in the interface
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
UI elements can be
either enabled (allowing
the user to interact with
them), or disabled, in
which case they’re
present and visible, but
“deactivated” or
(“grayed out”)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
What about the controls
that we can’t select with
ALT?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Introduction
When a horizontal
S l i d e r has the focus,
the LEFT / RIGHT
arrow keys change its
value.
When ComboBoxhas
the focus, the F4 key
will make the box
alternate between its
dropped-down and
collapsed views
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Label
The Label class defines labels.
17
18
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, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Button Example
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
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, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
CheckBox Example
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
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 selected).
26
RadioButton
RadioButton Example
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28
TextField
A text field can be used to enter or display a string. TextField is a
subclass of TextInputControl.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29
TextField
TextField
TextFieldDemo Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
32
TextArea
A TextArea enables the user to enter multiple lines of text.
If you want to let the user enter multiple lines of text, you may create
several instances of TextField. A better alternative,is to use TextArea
33
TextArea
TextArea Example
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
36
ComboBox
ComboBox is defined as a generic class like the ArrayList class. The generic
type T specifies the element type for the elements stored in a combo box.
ComboBox Example
This example lets users view an image and a
description of a country's flag by selecting the
country from a combo box.
ComboBoxDemo Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
38