JavaFX GUI Programming Essentials
JavaFX GUI Programming Essentials
vn 2
Outline
1. JavaFX Overview
OBJECT-ORIENTED PROGRAMMING
2. MVC Architectural Pattern
10. GUI PROGRAMMING WITH JAVAFX
3. JavaFX Window Structure
Nguyen Thi Thu Trang 4. Binding Properties
trangntt@[Link]
5. Data-Driven UIs
1 2
3 4
• Rich Internet applications (RIA) are Web- • Integrate 3D graphics, charts, and audio, video, and
based applications that have some characteristics of embedded Website inside GUI
graphical desktop applications • Easy to develop Game/Media applications
3 4
5 6
5 6
7 8
7 8
9
Model-View-Controller Controller
- responds to events
- accesses model / view
• Model
- propagates data
• Manageing the data of the application, independent of Model and view
the user interface
• View View
Model
• Rendering presentation of the data (from model) in a - long term storage - tables to display
particular format to user - data members - forms to change
• Controller - domain logic - controls to listen for events
• Accepting and responding to the user input
• logic to process user inputs
• Performs interactions on the data model objects
Model-View-Controller
• Updating views and/or showing data on views
Should probably be Model-Controller-View
9 10
11 12
11 12
13 14
13 14
18 19
20
20 21
22 23
22 23
24
24 25
27
26 27
28 29
28 29
30 31
30 31
32 33
32 33
34 35
FlowPane GridPane
ShowGridPane
Run
34 35
36 37
BorderPane HBox
36 37
38 39
VBox Shapes
JavaFX provides many shape classes for drawing texts,
lines, circles, rectangles, ellipses, arcs, polygons, and
polylines.
38 39
40 41
40 41
42 43
Line Rectangle
42 43
44 45
44 45
46 47
Ellipse Arc
radiusX radiusY
(centerX, centerY)
46 47
48 49
radiusY length
startAngle
0 degree
radiusX
(centerX, centerY)
–30° –50°
The getter and setter methods for property values and a getter for property
–20° 20°
[Link] itself are provided in the class, but omitted in the UML diagram for brevity.
(a) Negative starting angle –30° and (b) Negative starting angle –50° +Polygon() Creates an empty polygon.
negative spanning angle –20° and positive spanning angle 20° +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>
48 49
50 51
50 51
52 53
Bidirectional Binding
[Link]().
bindBidirectional([Link]());
52 53
54 55
56 57
Building the GUI (cont.) Building the GUI (cont.)
• Step 3: Adding the Controls • Step 4: Configuring the Sliders
• Add the Labels, Sliders, TextFields, a Circle and a • For the red, green and blue Sliders, set the Max
Rectangle to the GridPane—Circle and Rectangle are properties to 255 (the maximum amount of a given color
located in the Scene Builder Library’s Shapes section in the RGBA color scheme)
• When adding the Circle and Rectangle, place both into • For the alpha Slider, set its Max property to 1.0 (the
the rightmost column’s first row maximum opacity in the RGBA color scheme).
• Be sure to add the Circle before the Rectangle so that it • Step 5: Configuring the TextFields
will be located behind the rectangle in the layout • Set all of the TextField’s Pref Width properties to 50.
• Set the text of the Labels and TextFields as shown and
set all the appropriate fx:id properties as you add each
control.
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
58 59
60 61
ColorChooser Main Application
62 63
64 65
ColorChooserController (cont.) ColorChooserController (cont.)
66 67
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
68 69
ColorChooserController Class (cont.) ColorChooserController Class (cont.)
Property-to-Property Bindings (cont.) Property Listeners
• Slider’s valueProperty method returns the Slider’s • To perform an arbitrary task when a property’s value
value property as a DoubleProperty—an observable changes, register a property listener
double value • Lines 41–80 register property listeners for the Sliders’
• Because the TextField’s text property must be bound to a value properties
String, we call DoubleProperty method asString, • Consider lines 41–50, which register the ChangeListener
which returns a StringBinding object (an that executes when the user moves the redSlider’s thumb
ObservableValue) that produces a String representation
• Each ChangeListener stores the int value of the
of the DoubleProperty
newValue parameter in a corresponding instance variable,
• This version of asString receives a format-control String then calls the colorRectangle’s setFill method to
specifying the DoubleProperty’s format change its color, using Color method rgb to create the new
Color object
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
70 71
72
72 73
Cover Viewer application ListView and ObservableList
• Binds a list of Book objects to a ListView • ListView control:
Display a collection of book titles
• When the user selects an item in the ListView, the • Bind an ObservableList object to the ListView
corresponding Book’s cover image is displayed in an
ImageView. • If you make changes to an ObservableList, its observer
(the ListView in this app) will automatically be notified of
those changes
• Property listener to display the correct image
when the user selects an item from the
ListView—in this case, the property that changes
is the selected item
74 75
76 77
Building the GUI (cont.) Building the GUI (cont.)
Adding and Configuring the Controls (cont.) Specifying the Controller Class’s Name
• For the ImageView, set the Fit Width and Fit Height • To ensure that an object of the controller class is created
properties to 370 and 480, respectively when the app loads the FXML file at runtime, specify
• To size the BorderPane based on its contents, set its Pref CoverViewerController as the controller class’s name in
Width and Pref Height to USE_COMPUTED_SIZE the FXML file as you’ve done previously.
• Also, set the Padding property to 8 to inset the BorderPane Generating a Sample Controller Class
from the stage. • Select View > Show Sample Controller Skeleton, then copy this
code into a [Link] file and store
the file in the same folder as [Link]
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
78 79
80 81
CoverViewerController Class (cont.)
82 83
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
84 85
CoverViewerController Class (cont.) CoverViewerController Class (cont.)
Listening for ListView Selection Changes Listening for ListView Selection Changes (cont.)
• To synchronize the book cover that’s being displayed with the • ListView method getSelectionModel returns a
currently selected book, we listen for changes to the ListView’s MultipleSelectionModel object
selected item • In this example, MultipleSelectionModel’s
• By default a ListView supports single selection selectedItemProperty method returns a
• ListViews also support multiple selection ReadOnlyObjectProperty<Book>
• Selection is managed by the ListView’s • Corresponding ChangeListener receives as oldValue and
MultipleSelectionModel (a subclass of SelectionModel newValue the previously selected and newly selected Book
from package [Link] objects, respectively
• Contains observable properties and various methods for manipulating the • Lines 47–48 use newValue’s large image path to initialize a new
corresponding ListView’s items
Image (package [Link])—this loads the image
• To respond to selection changes, you register a listener for the from that path
MultipleSelectionModel’s selectedItem property (lines 41–
• We then pass the new Image to the coverImageView’s
51) setImage method to display the Image
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
86 87
88
More examples/tutorials
• [Link]
• [Link]
88