Wim Wiltenburg & Mark de Haan 1 Contents • What is JavaFX? • Installing JavaFX • Adding JavaFX to a project • A simple starting application • Running the application
2 Java Fundamentals - Wim Wiltenburg & Mark de Haan
What is JavaFX? JavaFX is a cross platform Java Graphical User Interface library. The first GUI library was AWT, which later evolved into Swing. JavaFX has WebView, which is an embedded browser component, that allows you to use HTML5 features, like Canvas, JavaScript and CSS Since JDK 11 JavaFX is no longer part of the JDK, but has to be included as a separate library, which is maintained by the OpenJFX project
3 Java Fundamentals - Wim Wiltenburg & Mark de Haan
Application architecture JavaFX is a flexible framework, enabling patterns such as MVC or MVVM to be used. This means we have to think about our code architecture. A simple approach to get started could look like this:
In this lecture, we will focus on how a View and Controller can work together. Creating a project
5 Java Fundamentals - Wim Wiltenburg & Mark de Haan
Running the application After generating the project, you should be able to run it. A simple HelloWorld example will appear.
So how does this work?
6 Java Fundamentals - Wim Wiltenburg & Mark de Haan Project setup • The view we are seeing, is defined in hello-view.fxml, which you can find in the resources folder. • In the view code, you can see it is linked to a controller class called HelloController, which is a separate .java file that is (unsurprisingly) called HelloController.java. • And finally, the application startup code is defined in HelloApplication.java. This startup code calls the FXMLLoader class (part of JavaFX) to load in the view and display it to the user.
7 Java Fundamentals - Wim Wiltenburg & Mark de Haan
Important note It’s important to know that we are not required to use FXML to use JavaFX. It is perfectly possible to write the entire UI in Java. However, using FXML enables us to use a tool called SceneBuilder for UI creation, and have a good separation of logic and view by default.
8 Java Fundamentals - Wim Wiltenburg & Mark de Haan