5. public class Hello extends Application {
@Override
public void start(Stage stage) {
// Create Container
Group container = new Group();
// Create root of Scene Graph
Scene scene = new Scene(container, 100, 20);
stage.setScene(scene);
// Create label and add it to Scene Graph
Label label = new Label("Hello, World!");
container.getChildren().add(label);
}
stage.show(); SceneGraph
public static void main(String[] args) {
launch(args);
}
} Start JavaFX Application Thread
6. Pitfalls
Add to Container
pane.add(comp);
pane.getChildren().add(comp);
Event
Only One Event Listener: EventHandler
button.setOnAction(
new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
// handling event
}
});
7. Pitfalls View Observer Pattern
Bind
Model
Swing
Event
Controller
8. Pitfalls View
Bind
Model
JavaFX Bind
Controller
9. Pitfalls View
Bind
Model
JavaFX Bind
Controller
// Model object
DoubleProperty xProperty = new DoubleProperty();
Slider slider = new Slider(50, 300, 0);
// Bind slider property to model object
xProperty.bind(slider.valueProperty());
Rectangle rect = new Rectangle(50, 10, 50, 30);
// Bind model object to x property of rectangle
rect.xProperty().bind(xProperty);
10. Migration Scenarios
Scenario #1 JavaFX in Swing
Scenario #2 Swing to JavaFX
w/o FXML
Scenario #3 Swing to JavaFX
w/ FXML
11. JavaFX in Swing
Embed JavaFX scene graph
in Swing application
No support for
embedding Swing components
into JavaFX applications
Use features JavaFX only has
with Swing
12. JavaFX in Swing
Embed JavaFX scene graph
in Swing application
No support for
embedding Swing components
into JavaFX applications
Use features JavaFX only has
Web Media
with Swing
Charts Animation
13. JFXPanel
JFXPanel is a Swing component
that is compatible with JavaFX
Add scene graph into JFXPanel
final JFXPanel fxPanel = new JFXPanel(); Swing EDT
jframe.add(fxPanel);
Platform.runLater(new Runnable() {
@Override
public void run() { JavaFX App Thread
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root);
root.getChildren().add(new Button("JavaFX Button"));
fxPanel.setScene(scene);
}
});
14. Swing to JavaFX w/o FXML
Rewrite Swing as JavaFX
Same concept, similar usage
Label, Button, TextField, et al.
Same concept, different usage
Layout
New concept
TableView, TreeView, ListView
15. Same concept, : Button
similar usage
Swing
JButton = new JButton(”Swing”);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(new ActionEvent event) {
// handling event
}
});
JavaFX
Button = new Button(”JavaFX”);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// handling event
}
});
16. Same concept, : Layout
different usage
Swing: Container+LayoutManeger
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(comp, BorderLayout.CENTER);
JavaFX: Container includes layout
BorderPane pane = new BorderPane();
pane.setCenter(comp);
17. New Concept: Table
Swing: TableModel
JavaFX: Bind Java Beans to Column
class Student { ...... } // Java Bean & gradYear props
{..} // Student has name
TableView<Student> table = new TableView<>();
TableColumn col1 = new TableColumn("Name");
col1.setCellValueFactory(
new PropertyValueFactory<Student, String>("name")
);
TableColumn col2 = new TableColumn("Grad Year");
col2.setCellValueFactory(
new PropertyValueFactory<Student, Integer>("gradYear")
);
table.setItems(students);
table.getColumns().addAll(col1, col2);
19. FXML
Use XML to describe GUI structure
FXML describes Scene Graph
Schema-less
Class: XML Element
Property: XML Attribute or Element
FXML read by Java application
at run time
26. Conclusion
3 migration scenarios
Not so different from
a Component-based UI standpoint
FXML is a key feature
No mention in this session
CSS, Animaction, Effect
Shape, Aysnc task
27. From Swing
To JavaFX
- Migration Guide to JavaFX -
Java in the Box
Yuichi Sakuraba