Getting started with javafx
Remarks#
JavaFX is a software platform for creating and delivering desktop applications, as well as rich internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE.
IT enables developers to design, create, test, debug, and deploy rich client applications.
The appearance of JavaFX applications can be customized using Cascading Style Sheets (CSS) for styling (see JavaFX: CSS) and (F)XML files can be used to object structures making it easy to build or develop an application (see FXML and Controllers). Scene Builder is a visual editor allowing the creation of fxml files for an UI without writing code.
Versions#
Version | Release Date |
---|---|
JavaFX 2 | 2011-10-10 |
JavaFX 8 | 2014-03-18 |
Installation or Setup
The JavaFX APIs are available as a fully integrated feature of the Java SE Runtime Environment (JRE) and the Java Development Kit (JDK ). Because the JDK is available for all major desktop platforms (Windows, Mac OS X, and Linux), JavaFX applications compiled to JDK 7 and later also run on all the major desktop platforms. Support for ARM platforms has also been made available with JavaFX 8. JDK for ARM includes the base, graphics and controls components of JavaFX.
To install JavaFX install your chosen version of the Java Runtime environment and Java Development kit.
Features offered by JavaFX include:
- Java APIs.
- FXML and Scene Builder.
- WebView.
- Swing interoperability.
- Built-in UI controls and CSS.
- Modena theme.
- 3D Graphics Features.
- Canvas API.
- Printing API.
- Rich Text Support.
- Multitouch Support.
- Hi-DPI support.
- Hardware-accelerated graphics pipeline.
- High-performance media engine.
- Self-contained application deployment model.
Hello World program
The following code creates a simple user interface containing a single Button
that prints a String
to the console on click.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) {
// create a button with specified text
Button button = new Button("Say 'Hello World'");
// set a handler that is executed when the user activates the button
// e.g. by clicking it or pressing enter while it's focused
button.setOnAction(e -> {
//Open information dialog that says hello
Alert alert = new Alert(AlertType.INFORMATION, "Hello World!?");
alert.showAndWait();
});
// the root of the scene shown in the main window
StackPane root = new StackPane();
// add button as child of the root
root.getChildren().add(button);
// create a scene specifying the root and the size
Scene scene = new Scene(root, 500, 300);
// add scene to the stage
primaryStage.setScene(scene);
// make the stage visible
primaryStage.show();
}
public static void main(String[] args) {
// launch the HelloWorld application.
// Since this method is a member of the HelloWorld class the first
// parameter is not required
Application.launch(HelloWorld.class, args);
}
}
The Application
class is the entry point of every JavaFX application. Only one Application
can be launched and this is done using
Application.launch(HelloWorld.class, args);
This creates a instance of the Application
class passed as parameter and starts up the JavaFX platform.
The following is important for the programmer here:
- First
launch
creates a new instance of theApplication
class (HelloWorld
in this case). TheApplication
class therefore needs a no-arg constructor. init()
is called on theApplication
instance created. In this case the default implementation fromApplication
does nothing.start
is called for theAppication
instance and the primaryStage
(= window) is passed to the method. This method is automatically called on the JavaFX Application thread (Platform thread).- The application runs until the platform determines it’s time to shut down. This is done when the last window is closed in this case.
- The
stop
method is invoked on theApplication
instance. In this case the implementation fromApplication
does nothing. This method is automatically called on the JavaFX Application thread (Platform thread).
In the start
method the scene graph is constructed. In this case it contains 2 Node
s: A Button
and a StackPane
.
The Button
represents a button in the UI and the StackPane
is a container for the Button
that determines it’s placement.
A Scene
is created to display these Node
s. Finally the Scene
is added to the Stage
which is the window that shows the whole UI.