Java FX
Java FX
Feature Description
1import javafx.application.Application;
2import javafx.event.ActionEvent;
3import javafx.event.EventHandler;
4import javafx.scene.Scene;
5import javafx.scene.control.Button;
6import javafx.scene.layout.StackPane;
7import javafx.stage.Stage;
8
9public class JavaFXHello extends Application {
10 private Button btnHello; // Declare a "Button" control
11
12 @Override
13 public void start(Stage primaryStage) {
14 // Construct the "Button" and attach an "EventHandler"
15 btnHello = new Button();
16 btnHello.setText("Say Hello");
17 // Using JDK 8 Lambda Expression to construct an EventHandler<A
18 btnHello.setOnAction(evt -> System.out.println("Hello World!"));
19
20 // Construct a scene graph of nodes
21 StackPane root = new StackPane(); // The root of scene graph is a la
22 root.getChildren().add(btnHello); // The root node adds Button as a
23
24 Scene scene = new Scene(root, 300, 100); // Construct a scene given
25 primaryStage.setScene(scene); // The stage sets scene
26 primaryStage.setTitle("Hello"); // Set window's title
27 primaryStage.show(); // Set visible (show it)
28 }
29
30 public static void main(String[] args) {
31 launch(args);
32 }
33}
How It Works
1. A JavaFX GUI Program extends
from javafx.application.Application (just like a Java Swing
GUI program extends from javax.swing.JFrame).
2. JavaFX provides a huge set of controls (or components) in
package javafx.scene.control,
including Label, Button and TextField.
3. We declare and construct a Button control, and attach
a javafx.event.EventHandler<ActionEvent> to the Button, via
method setOnAction() (of ButtonBase superclass), which
takes an EventHandler<ActionEvent>, as follows:
public final void setOnAction(EventHandler<ActionEvent>
value)
The EventHandler is a Functional Interface with an abstract
method handle(), defined as follows:
package javafx.event;
@FunctionalInterface
public interface EventHandler<T extends Event> extends
EventListener {
void handle(T event); // public abstract
}
We can trigger the handle() by firing the button, via clicking
the button with the mouse or touch, key press, or invoke
the fire() method programmatically.
In this example, we use a one-liner Lambda Expression (JDK
8) to construct an instance of Functional
Interface EventHandler. You can also use an anonymous
inner class (Pre JDK 8), as follows:
btnHello.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent evt) {
System.out.println("Hello World!");
}
});
4. JavaFX uses the metaphor of a theater to model the graphics
application. A stage (defined by the javafx.stage.Stage class)
represents the top-level container (window). The individual
controls (or components) are contained in a scene (defined by
the javafx.scene.Scene class). An application can have more
than one scenes, but only one of the scenes can be displayed
on the stage at any given time. The contents of a scene is
represented in a hierarchical scene graph of nodes (defined
by javafx.scene.Node).