JavaFX Scene
Jakob Jenkov |
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
javafx.scene.Scene
. A 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 Scene
object and add GUI components to it.
Create Scene
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 Scene
object:
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 Stage
.
Here is an example of setting a JavaFX Scene
on a Stage
:
VBox vBox = new VBox(new Label("A JavaFX Label")); Scene scene = new Scene(vBox); Stage stage = new Stage(); stage.setScene(scene);
A JavaFX 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 Scene
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 Scene
via the setCursor()
method. Here is an example of setting the mouse cursor of a
JavaFX Scene
:
scene.setCursor(Cursor.OPEN_HAND);
The 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:
- Cursor.OPEN_HAND
- Cursor.CLOSED_HAND
- Cursor.CROSSHAIR
- Cursor.DEFAULT
- Cursor.HAND
- Cursor.WAIT
- Cursor.H_RESIZE
- Cursor.V_RESIZE
- Cursor.MOVE
- Cursor.TEXT
There are a few more. Just play with the constants found in the Cursor
class and see for yourself.
Tweet | |
Jakob Jenkov |