Hello World with JavaFx
JavaFx is a comprehensive platform and set of APIs to develop desktop, web and even mobile applications. Although intended to replace the Swing GUI API, it can get significantly more complex, with FXML for XML based object specification; CSS styling; and special IDE tool chains.
Since JDK 8, it is supposedly included in the main install, however, on Debian I had to install it separately:
apt-get install openjfx openjfx-source
The Oracle documentation and tutorial is detailed and well written, so I will not go into further detail here. The following is a simple Hello World app.
JavaFxHelloWorld.java
/* Copyright rememberjava.com. Licensed under GPL 3. See http://rememberjava.com/license */
package com.rememberjava.ui;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@SuppressWarnings("restriction")
public class JavaFxHelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("My App");
Label label = new Label();
Button button = new Button();
button.setText("Say 'Hello World'");
button.setOnAction((e) -> {
label.setText("Hi!");
});
VBox root = new VBox();
ObservableList<Node> children = root.getChildren();
children.add(button);
children.add(label);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}