Programming With Javafx: Tecniche Di Programmazione - A.A. 2016/2017
Programming With Javafx: Tecniche Di Programmazione - A.A. 2016/2017
Introduction to JavaFX
GUI in Java
} Graphic framework available in Java
} AWT (1996)
} Swing (1998)
} Extremely powerful, many extensions available
} Complex to master, requires low-level handling
} Hard to create visually pleasing applications
} Alternatives available
} Most notable: SWT (Eclipse)
} Still cumbersome to master
Introduction to JavaFX
Separation of concerns
@Override
public void start(Stage stage) {
Group root = new Group(); // the root is Group or Pane
Scene scene = new Scene(root, 500, 500, Color.BLACK);
stage.setTitle("JavaFX Demo");
stage.setScene(scene);
stage.show();
}
http://jfxtras.org/
@Override
public void start(Stage stage) {
Group root = new Group();
stage.setTitle("JavaFX Demo");
stage.setScene(scene);
stage.show();
}
}
18 Tecniche di programmazione A.A. 2016/2017
JavaFX Scene Builder 8.1
http://gluonhq.com/open-source/scene-builder/
. . .
stage.setTitle("Circle Demo");
stage.setScene(new Scene(root, 500, 150));
stage.show();
}
Introduction to JavaFX
Empty JavaFX window
public class Main extends Application {
@Override
public void start(Stage stage) {
Group root = new Group(); // the root is Group or Pane
Scene scene = new Scene(root, 500, 500, Color.BLACK);
stage.setTitle("JavaFX Demo");
stage.setScene(scene);
stage.show();
}
.setScene
extends .start()
creates
Scene
MyApp Root node
children (at constructor)
creates
Node
adds
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
root.getChildren().add(btn);
javafx.stage.Stage javafx.scene.Scene
} The JavaFX Stage class is the } The container for all content
top level JavaFX container. in a scene graph
} The primary Stage is } The application must specify
constructed by the platform. the root Node for the scene
} Additional Stage objects may graph
be constructed by the } Root may be Group (clips),
application. Region, Control (resizes)
} A stage can optionally have } If no initial size is specified, it
an owner Window. will automatically compute it
Introduction to JavaFX
Nodes
} Root node: top level container
} Intermediate nodes:
} Containers
} Layout managers
} UI Composite controls
} Leaf (terminal) nodes:
} Shapes
} UI Controls
} Organized as a Hierarchical tree
Nodes family
CheckBox
ButtonBase
MenuButton
Cell
Labeled ToggleButton
Label
ListView
Control TitledPane
Group
MenuBar
Focus on
Slider Panes
TabPane and
TextInputControl
TextArea
Controls
TextField
Parent
ToolBar
AnchorPane
TreeView
BorderPane
Axis
FlowPane
Region Chart
GridPane
WebView Pane
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Hello Devoxx");
Group root = new Group();
Scene scene = new Scene(root, 400, 250,
Color.ALICEBLUE);
Text text = new Text();
text.setX(105);
text.setY(120);
text.setFont(new Font(30));
text.setText("Hello Devoxx");
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.show();
}
}
50 Tecniche di programmazione A.A. 2016/2017
public void start(Stage primaryStage)
{
primaryStage.setTitle("Hello Devoxx");
primaryStage.setScene(SceneBuilder.create()
.width(400).height(250).fill(Color.ALICEBLUE)
.root(GroupBuilder.create().children(
TextBuilder.create()
.x(105).y(120)
.text("Hello Devoxx")
.font(new Font(30)).build()
).build()
).build());
primaryStage.show();
}
@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();
}
Introduction to JavaFX
Interacting with Nodes
} In JavaFX applications, events are notifications that
something has happened.
} An event represents an occurrence of something of interest to
the application
} As a user clicks a button, presses a key, moves a mouse, or
performs other actions, events are dispatched.
} Registered event filters and event handlers within the
application
} receive the event and
} provide a response.
@Override
public void handle(ActionEvent event) {
Button b = (Button)event.getSource() ;
//...do something
String buttonText = b.getText() ;
// ...
} Registration
}
Button btn = new Button() ;
btn.setOnAction(new ButtonActionHandler()) ;
63 Tecniche di programmazione A.A. 2016/2017
Example (inline definition)
Registration &
Anonymous event handler
btn.setOnAction(new EventHandler<ActionEvent>() {
});
JavaFX programming
Application complexity and MVC
} Interactive, graphical applications exhibit complex
interaction patterns
} Flow of control is in the hand of the user
} Actions are mainly asynchronous
JavaFX programming
The Controller in FXML
} Several attributes in FXML help in the definition of the
Controller behavior associated to a scene
} Identification of the Controller class
} Injection of Node identifiers (references)
} Registration of event handlers
} Additionally, the JavaFX Scene Builder may generate a
«controller skeleton» for inclusion in the project
MyController controller =
(MyController)fxmlLoader.getController();
JavaFX programming
JavaFX Properties
} Modern revisitation of the JavaBeans framework (back
from Java 1.1)
} Easy to connect different variable values
} Some may be internal variables
} Some may be visual elements
} Supports automatic «binding» to efficiently propagate
changes
} Simplifies programming
Introduction to JavaFX
Resources
} Official
} http://www.oracle.com/us/technologies/java/fx/overview/index.
html
} http://www.oracle.com/technetwork/java/javafx/overview/index
.html
} Documents
} http://docs.oracle.com/javafx/
} http://docs.oracle.com/javafx/2/api/index.html
} Blogs
} http://fxexperience.com/
} http://www.learnjavafx.typepad.com/weblog/
} http://community.java.net/community/javafx