01 Javafx Programming
01 Javafx Programming
Summary
1.
2.
3.
4.
5.
6.
7.
8.
9.
Tecniche di programmazione
A.A. 2013/2014
GUI in Java
Graphic framework available in Java
Swing
Extremely powerful, many extensions available
Complex to master, requires low-level handling
Hard to create visually pleasing applications
Alternatives available
Tecniche di programmazione
A.A. 2013/2014
Tecniche di programmazione
A.A. 2013/2014
JavaFX 2.x
Redesigned from scratch
The JavaFX 2.x framework is entirely written in Java
For visual layout, an XML file may also be used (called
FXML)
Graphic appearance borrows from web-standard CSS
style sheets
UI programming is based on easy to handle events and
bindings
Tecniche di programmazione
A.A. 2013/2014
Tecniche di programmazione
A.A. 2013/2014
Basic concepts
Introduction to JavaFX
Tecniche di programmazione
A.A. 2013/2014
10
Tecniche di programmazione
A.A. 2013/2014
BorderPane (5-areas)
Hbox,Vbox (linear sequence)
StackPane (overlay all children)
GridPane (row x columns)
FlowPane (flowing boxes, wrap around)
TilePane (flowpane with equally sized boxes)
AnchorPane (magnetically attach nodes at corners or
sides)
11
Tecniche di programmazione
A.A. 2013/2014
12
Tecniche di programmazione
A.A. 2013/2014
ChoiceBox
ColorPicker
ComboBoxBase
Nodes family
Button
ComboBox
CheckBox
ButtonBase
MenuButton
Cell
Labeled
ToggleButton
Label
ListView
TitledPane
Control
MenuBar
Group
Separator
Slider
TabPane
TextArea
TextInputControl
Parent
TextField
ToolBar
AnchorPane
TreeView
BorderPane
Axis
FlowPane
Region
Chart
WebView
Pane
GridPane
HBox
javafx.scene.Node
Focus on
Panes
and
Controls
Arc
StackPane
Circle
TilePane
Line
VBox
Shape
Polygon
Canvas
Polyline
Imageview
Rectangle
Text
13
Tecniche di programmazione
A.A. 2013/2014
14
Tecniche di programmazione
A.A. 2013/2014
15
Tecniche di programmazione
A.A. 2013/2014
Tecniche di programmazione
A.A. 2013/2014
In Java code
In FXML
17
Tecniche di programmazione
A.A. 2013/2014
18
Tecniche di programmazione
A.A. 2013/2014
FXML fragment
. . .
<HBox id="HBox" alignment="CENTER" spacing="15.0"
AnchorPane.rightAnchor="23.0" AnchorPane.topAnchor="22.0">
<children>
<Button id="button1" fx:id="newIssue" onAction="#newIssueFired"
text="New" />
<Button id="button2" fx:id="saveIssue" onAction="#saveIssueFired"
text="Save" />
<Button id="button3" fx:id="deleteIssue" onAction="#deleteIssueFired"
text="Delete" />
</children>
</HBox>
<ImageView id="IssueTrackingLite" layoutX="14.0" layoutY="20.0">
<image>
<Image url="@IssueTrackingLite.png" preserveRatio="true" smooth="true" />
</image>
</ImageView>
. . .
19
Tecniche di programmazione
A.A. 2013/2014
20
Tecniche di programmazione
A.A. 2013/2014
Application structure
Introduction to JavaFX
Separation of concerns
22
Tecniche di programmazione
A.A. 2012/2013
24
Tecniche di programmazione
A.A. 2013/2014
General rules
25
A.A. 2012/2013
Minimal example
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Tecniche di programmazione
A.A. 2012/2013
javafx.scene.Scene
Tecniche di programmazione
A.A. 2012/2013
Nodes
Layout components
UI Controls
Charts
Shapes
UI events
Tecniche di programmazione
A.A. 2012/2013
Events
FX Event (javafx.event.Event):
29
Tecniche di programmazione
A.A. 2012/2013
Properties
30
Tecniche di programmazione
A.A. 2012/2013
Bindings
Tecniche di programmazione
A.A. 2012/2013
Nodes
Containers
Layout managers
UI Composite controls
Shapes
UI Controls
33
Tecniche di programmazione
A.A. 2012/2013
ChoiceBox
ColorPicker
ComboBoxBase
Button
ComboBox
Nodes family
CheckBox
ButtonBase
MenuButton
Cell
Labeled
ToggleButton
Label
ListView
Control
TitledPane
Focus on
Panes
and
Controls
MenuBar
Group
Slider
TabPane
TextArea
TextInputControl
TextField
Parent
ToolBar
AnchorPane
TreeView
BorderPane
Axis
FlowPane
Region
Chart
WebView
Pane
GridPane
JavaDoc
is your
friend
HBox
javafx.scene.Node
Arc
StackPane
Circle
TilePane
Line
Shape
VBox
Polygon
Canvas
Rectangle
34
Imageview
Text
Tecniche di programmazione
A.A. 2012/2013
35
Tecniche di programmazione
A.A. 2012/2013
UI Form Controls
Controls may be
combined to construct
Forms
Control Nodes have a
value property
36
Button: ActionEvent
Text: ActionEvent,
KeyTyped, KeyPressed,
MouseClicked, ...
Tecniche di programmazione
A.A. 2012/2013
37
Tecniche di programmazione
A.A. 2012/2013
Group:
Region:
Control:
38
A.A. 2012/2013
39
Tecniche di programmazione
A.A. 2012/2013
40
Tecniche di programmazione
A.A. 2012/2013
41
Tecniche di programmazione
A.A. 2012/2013
42
Tecniche di programmazione
A.A. 2012/2013
43
Tecniche di programmazione
A.A. 2012/2013
44
Tecniche di programmazione
A.A. 2012/2013
45
Tecniche di programmazione
A.A. 2012/2013
46
Tecniche di programmazione
A.A. 2012/2013
47
Tecniche di programmazione
A.A. 2012/2013
root.getChildren().add(text);
48
Tecniche di programmazione
A.A. 2012/2013
49
Tecniche di programmazione
A.A. 2012/2013
50
Tecniche di programmazione
A.A. 2012/2013
XML-based format
Nested tree of XML Elements, corresponding to Nodes
XML Attributes corresponding to (initial) properties of
nodes
51
Tecniche di programmazione
A.A. 2012/2013
Example
52
Tecniche di programmazione
A.A. 2012/2013
53
Tecniche di programmazione
A.A. 2012/2013
FXMLLoader
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(
getClass().getResource("fxml_example.fxml"));
stage.setTitle("FXML Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();
}
54
Tecniche di programmazione
A.A. 2012/2013
scene.lookup("#myId");
55
Tecniche di programmazione
A.A. 2012/2013
Events
Introduction to JavaFX
57
Tecniche di programmazione
A.A. 2012/2013
What is an event?
58
Tecniche di programmazione
A.A. 2012/2013
Event propagation
Tecniche di programmazione
A.A. 2012/2013
Event Handlers
60
Tecniche di programmazione
A.A. 2012/2013
setOnEvent-type(
EventHandler<? super event-class> value )
Event-Type
Event-class
The class that defines the event type (e.g., KeyEvent , MouseEvent, ...)
Value
61
The event handler for event-class (or for one of its super classes)
Must implement: public void handle(ActionEvent event)
May be a regular class or an anonymous inline class
Tecniche di programmazione
A.A. 2012/2013
Example
class ButtonActionHandler implements
javafx.event.EventHandler<ActionEvent> {
Event Handler
Registration
Tecniche di programmazione
A.A. 2012/2013
63
Tecniche di programmazione
A.A. 2012/2013
Model-View-Controller
JavaFX programming
Tecniche di programmazione
A.A. 2012/2013
66
Tecniche di programmazione
A.A. 2012/2013
67
Tecniche di programmazione
A.A. 2012/2013
68
Tecniche di programmazione
A.A. 2012/2013
FXML
The Nodes in the Scene Graph
Set of event handlers
69
Tecniche di programmazione
A.A. 2012/2013
Design Exercise
70
Tecniche di programmazione
A.A. 2012/2013
A possible
solution
71
Tecniche di programmazione
A.A. 2012/2013
73
Tecniche di programmazione
A.A. 2012/2013
74
<BorderPane
id="BorderPane"
xmlns:fx="http://javafx.com
/fxml"
fx:controller="it.polito.te
cnprogr.RuzzleController">
Tecniche di programmazione
A.A. 2012/2013
fx:controller attribute
75
Tecniche di programmazione
A.A. 2012/2013
76
Tecniche di programmazione
A.A. 2012/2013
Tecniche di programmazione
A.A. 2012/2013
<Button fx:id="cercaBtn"
onAction="#doCercaParola"
text="Cerca" />
78
@FXML
void doCercaParola (
ActionEvent event ) {
Tecniche di programmazione
A.A. 2012/2013
Resources
Introduction to JavaFX
Resources
Official
Documents
http://www.oracle.com/us/technologies/java/fx/overview/index.
html
http://www.oracle.com/technetwork/java/javafx/overview/index
.html
http://docs.oracle.com/javafx/
http://docs.oracle.com/javafx/2/api/index.html
Blogs
80
http://fxexperience.com/
http://www.learnjavafx.typepad.com/weblog/
http://community.java.net/community/javafx
Tecniche di programmazione
A.A. 2013/2014
Resources
API
Slides/Tips
http://www.slideshare.net/steveonjava/java-fx-20-a-developers-guide
http://refcardz.dzone.com/refcardz/getting-started-javafx
Tutorials/Articles
http://docs.oracle.com/javafx/2/api/index.html
http://docs.oracle.com/javafx/2/events/jfxpub-events.htm
http://amyfowlersblog.wordpress.com/2011/06/02/javafx2-0-layout-aclass-tour/
Examples (Downloads)
81
A.A. 2012/2013
Resources
FXML Controller
Charts
http://docs.oracle.com/javafx/2/api/javafx/fxml/docfiles/introduction_to_fxml.html#controllers
Using JavaFX Charts tutorial:
http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm
Books
82
Tecniche di programmazione
A.A. 2012/2013
Resources
83
http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
http://thierrywasyl.wordpress.com/2012/07/29/properties-andbindings-in-javafx/
Tecniche di programmazione
A.A. 2012/2013
Licenza duso
http://creativecommons.org/licenses/by-nc-sa/3.0/
84
Tecniche di programmazione
A.A. 2013/2014