Home » Desktop Java » JavaFX » JavaFX Scene Example

About Prasad Saya

Prasad Saya
Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.

JavaFX Scene Example

This is an example of JavaFX Scene class. The Scene class is the base container for all content in a scene graph. This class is defined in the javafx.scene package.

1. Overview

The following are the main properties for the scene class:

  • fill: The fill property specifies the background of the scene. The default fill of the Scene is Color.WHITE. The setFill(Paint value) method sets the value. The Paint is the base class for a color or gradients used to fill shapes and backgrounds when rendering the scene graph.
  • root: The root property specifies the application’s root Node for the scene graph. The root can be a Group or a resizable node (layout Region or Control). The setRoot(Parent value) method sets the root value. Note that a null cannot be specified for the root property.
  • size: The scene’s size may be initialized by the application during construction. If no size is specified, the scene will automatically compute its initial size based on the preferred size of its content.
  • depthBuffer and antiAliasing flags: These are conditional features.

Want to master JavaFX?

Subscribe to our newsletter and download the JavaFX Programming Cookbook right now!

In order to get you prepared for your JavaFX development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!


The root, fill, size properties can also be specified while constructing the scene as constructor arguments. An application may also specify depth buffer support or scene anti-aliasing support at the creation of a scene. Note that the scene objects must be constructed and modified on the JavaFX Application thread.

The scene also defines a number of functions to be called when an event occurs on this scene. The following are some of the events: MouseEvent, DragEvent, KeyEvent, Rotate, ScrollEvent etc… Some related functions are: onMouseClicked, onKeyTyped, onMouseDragged, onRotate, onScroll, etc.. For example, a KeyEvent indicates that a keystroke occurred in a Node. The scene’s setOnKeyTyped(EventHandler value) method sets the value of the property onKeyTyped.

The examples in this article show the usage of some properties including, fill, root, size and an event function.

2. Scene Example 1

This example shows the basic scene without any properties set. The default fill and size properties are set by the JavaFX system. The default fill of the scene Color.WHITE is seen here. The Group is specified as the root Node of the scene graph.

The following code snippet shows the construction:

Group root = new Group();
Scene scene = new Scene(root);	

The code when executed displays a window as shown below:

Figure 1 : Scene Example 1

Figure 1 : Scene Example 1

3. Scene Example 2

This example shows a scene with some properties (fill, size, cursor), and onMouseClicked function. The following code snippet shows the scene’s construction:

scene = new Scene(hbox, 600, 300);
scene.setOnMouseClicked(new MouseClickListener());

From the above code, note that the scene’s constructor specifies the size and the root properties. The root property type is a HBox layout.

The setOnMouseClicked() property is set with an event handler class MouseClickListener. The listener class implements EventHandler<MouseEvent> interface. In the GUI, when a mouse-click occurs in the scene area the scene’s background color (fill property) and the cursor’s shape are changed (Cursor defines the mouse cursor for this scene).

The following is the GUI for this example:

Figure 2: Scene Example 2

Figure 2: Scene Example 2

4. Download Java Source Code

This was an example of javafx.scene.Scene

You can download the full source code of this example here: SceneExamples.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns