The JavaFX Scene object is the root of the JavaFX Scene graph. In other words, the JavaFX Scene contains
all the visual JavaFX GUI components inside it. A JavaFX Scene is represented by the class
Scene object has to be set on a JavaFX Stage
to be visible. In this JavaFX
Scene tutorial I will show you how to create a
object and add GUI components to it.
You create a JavaFX
Scene object via its constructor. As parameter you must pass the root JavaFX
GUI component that is to act as the root view to be displayed inside the
Scene. Here is an example
of creating a JavaFX
VBox vBox = new VBox(); Scene scene = new Scene(vBox);
Set Scene on Stage
In order to make a JavaFX
Scene visible, it must be set on a JavaFX
Here is an example of setting a JavaFX
Scene on a
VBox vBox = new VBox(new Label("A JavaFX Label")); Scene scene = new Scene(vBox); Stage stage = new Stage(); stage.setScene(scene);
Scene can be attached to only a single
Stage at a time, and
Stage can also only display one
Scene at a time.
The Scene Graph
As mentioned in the JavaFX Overview, the scene graph consists of
all the nodes which are attached to a given JavaFX
Scene object. Each
object has its own scene graph.
The scene graph has a single root node. Other nodes can be attached to the root node in a tree-like data structure (a tree is a kind of graph).
Scene Mouse Cursor
It is possible to set the mouse cursor of a JavaFX
Scene. The mouse cursor is the little icon
that is being displayed at the location of the mouse cursor (pointer). You set the mouse cursor of a
setCursor() method. Here is an example of setting the mouse cursor of a
javafx.scene.Cursor class contains a lot of constants you can use to specify which mouse cursor
you want to display. Some of these constants are:
There are a few more. Just play with the constants found in the
Cursor class and see for yourself.