View RSS Feed

A JavaFX Blog

The building blocks of a JavaFX GUI: Stages, Scenes and Nodes

Rate this Entry
by , 12-11-2016 at 05:02 PM (5074 Views)
In JavaFX, the central metaphors used to represent a graphical user interface are the stage and the scene.

A stage is the “location” where graphic elements will be displayed; it corresponds to a window in a desktop environment. The javafx.stage.Stage class represents a stage; the so-called “primary stage” (which corresponds to the main window of an application) is created automatically by the JavaFX framework as soon as an application is launched, and is supplied as argument to the start() method of the Application class.

A scene is a “container” for the set of graphic elements to be displayed on a given stage. The graphic elements are called “nodes” and are organized in a scene according to a hierarchical tree structure, with one root node and a set of (direct or indirect) children of the root node; this tree structure is referred to as the scene graph. The Java class representing a scene is javafx.scene.Scene, while any graphic element is a subclass of the abstract class javafx.scene.Node. A scene graph must contain at least one node (the root node). When a scene is assigned to a stage, all the scene contents (defined by the scene graph) are displayed on the stage.

The following application shows a minimalistic scene:

Java Code:
public class SceneExample extends Application {
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Scene Example");
    FlowPane rootNode = new FlowPane();
    Label label = new Label("my label");
    rootNode.getChildren().add(label);
    Scene scene = new Scene(rootNode);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}
In this example, the root node of the scene graph is an instance of FlowPane (a Node subclass which arranges its children nodes as a “flow” of elements filling the available space starting from a corner), which contains a single child node (a Label object). The Scene object is created passing the root node as argument to the constructor, and the scene is assigned to the primary stage with the setScene() method. Finally, the stage is shown on the screen by calling show(). The following window is opened when this application is launched:

Name:  javafx-scene-example.png
Views: 2890
Size:  2.7 KB

The window title corresponds to the String argument passed to the setTitle() method called on the primary stage object.

Note: This example has been written using JavaFX 8.

Submit "The building blocks of a JavaFX GUI: Stages, Scenes and Nodes" to Facebook Submit "The building blocks of a JavaFX GUI: Stages, Scenes and Nodes" to Digg Submit "The building blocks of a JavaFX GUI: Stages, Scenes and Nodes" to del.icio.us Submit "The building blocks of a JavaFX GUI: Stages, Scenes and Nodes" to StumbleUpon Submit "The building blocks of a JavaFX GUI: Stages, Scenes and Nodes" to Google

Updated 12-11-2016 at 06:17 PM by JavaFX

Tags: None Add / Edit Tags
Categories
JavaFX Stage , JavaFX Scene , JavaFX Examples , JavaFX Node