JavaFX Scene Example
This is a JavaFXScene example. AScene represents the visual contents of aStage. TheScene class in thejavafx.scene package represents a scene in a JavaFX program.
AScene object is attached to, at the most, one stage at a time. If an already attached scene is attached to another stage, it is first detached from the previous stage. A stage can have, at the most, one scene attached to it at any time.
AScene contains a Scene Graph that consists of visual nodes. In this sense, a scene acts as a container for a Scene Graph. A Scene Graph is a tree data structure whose elements are known as nodes. Nodes in a Scene Graph form a parent-child hierarchical relationship.
ANode in a Scene Graph is an instance of thejavafx.scene.Node class. A node can be a branch node or a leaf node. A branch node can have children nodes, whereas a leaf node cannot. The first node in a Scene Graph is called the root node.
The following table shows an overview of the whole article:
Table Of Contents
The following examples use Java SE 7 and JavaFX 2.2.
1. What is a Scene
AScene always has a root node. If the root node is resizable, for example aRegion, it tracks the size of the scene. That is, if the scene is resized, the resizable root node resizes itself to fill the entire scene. Based on the policy of a root node, the Scene Graph may be laid out again when the size of the scene changes.
AGroup is a nonresizable parent node that can be set as the root node of a scene. If aGroup is the root node of a scene, the content of the Scene Graph is clipped by the size of the scene. If the scene is resized, the Scene Graph is not laid out again.
Parent is an abstract class. It is the base class for all branch nodes in a Scene Graph. If you want to add a branch node to a Scene Graph, use objects of one of its concrete subclasses, for example,Group,Pane,HBox, orVBox. Classes that are subclasses of theNode class, but not theParent class, represent leaf nodes, for example,Rectangle,Circle,Text,Canvas, orImageView.
The root node of a Scene Graph is a special branch node that is the topmost node. This is the reason you use aGroup or aVBox as the root node while creating aScene object.
2. Setting the Cursor for a Scene
2.1 The Code
FxSceneExample1.java
package FXScene;import javafx.application.Application;import javafx.scene.Cursor;import javafx.scene.Scene;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class FxSceneExample1 extends Application{public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the ImageString file = "file:///Path-To-Your-Image/javafx-logo.png";Image image = new Image(file);// Create the CursorCursor myCur = Cursor.cursor(file);// Create the ImageViewImageView imageView = new ImageView();// Add the Image to the ImageView imageView.setImage(image); // Create the VBoxVBox root = new VBox();// Set the width and height of the VBoxroot.setMinWidth(300);root.setMinHeight(200);// Add the ImageView to the VBoxroot.getChildren().add(imageView);// Set the Style-properties of the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Set the Cursor to the Scenescene.setCursor(myCur);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("Setting the Cursor for a Scene");// Display the Stagestage.show();}}An instance of thejavafx.scene.Cursor class represents a mouse cursor. TheCursor class contains many constants, for exampleHAND,CLOSED_HAND,DEFAULT,TEXT,NONE,WAIT, for standard mouse cursors.
The following snippet of code sets theWAIT cursor for aScene:
Scene scene;...scene.setCursor(Cursor.WAIT);
You can also create and set a custom cursor to a scene. Thecursor(String name) static method of theCursor class returns a standard cursor if the specified name is the name of a standard cursor. Otherwise, it treats the specified name as a URL for the cursor bitmap.
The following snippet of code creates a cursor from a bitmap file named javafx-logo.png.
// Create the ImageString file = "file:///C:/Workspaces/workspace_java/JavaFXProjects/src/FXScene/javafx-logo.png";Image image = new Image(file);// Create the CursorCursor myCur = Cursor.cursor(file);
2.2 The GUI
3. The Focus Owner in a Scene
3.1 The Code
FxSceneExample2.java
package FXScene;import javafx.application.Application;import javafx.scene.Node;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.TextArea;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class FxSceneExample2 extends Application{// Create the TextAreaTextArea textarea = new TextArea();public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the ButtonButton button = new Button("Test");// Create the VBoxVBox root = new VBox();// Set the width and height of the VBoxroot.setMinWidth(200);root.setMinHeight(100);// Add the children to the VBoxroot.getChildren().addAll(button, textarea);// Set the Focus Ownerbutton.requestFocus();// Create the SceneScene scene = new Scene(root);// Get the FocusOwnerNode focusOwnerNode = scene.getFocusOwner();if (focusOwnerNode == null) {// The scene does not have a focus ownerwriteMessage("The scene does not have a focus owner");}else if (focusOwnerNode.isFocused()) {// The focus owner is the one that has the focuswriteMessage("The focus owner is the one that has the focus");}else {// The focus owner does not have the focuswriteMessage("The focus owner does not have the focus");}// Set the Style-properties of the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("Testing the Focus Owner in a Scene");// Display the Stagestage.show();}private void writeMessage(String msg){textarea.appendText(msg + "\n");}}Only one node in a scene can be the focus owner. ThefocusOwner property of theScene class tracks theNode class that has the focus. Note that thefocusOwner property is read-only. If you want a specific node in a scene to be the focus owner, you need to call therequestFocus() method of theNode class.
The following line of code shows an example:
// Set the Focus Ownerbutton.requestFocus();
You can use thegetFocusOwner() method of theScene class to get the reference of the node having the focus in the scene. A scene may not have a focus owner, and in that case, thegetFocusOwner() method returns null. For example, a scene does not have a focus owner when it is created but is not attached to a window.
It is important to understand the distinction between a focus owner and a node having focus. Each scene may have a focus owner. For example, if you open two windows, you will have two scenes and you can have two focus owners. However, only one of the two focus owners can have the focus at a time.
The focus owner of the active window will have the focus. To check if the focus owner node also has the focus, you need to use the focused property of theNode class.
The following snippet of code shows the typical logic in using the focus owner:
// Get the FocusOwnerNode focusOwnerNode = scene.getFocusOwner();if (focusOwnerNode == null) {// The scene does not have a focus ownerwriteMessage("The scene does not have a focus owner");}else if (focusOwnerNode.isFocused()) {// The focus owner is the one that has the focuswriteMessage("The focus owner is the one that has the focus");}else {// The focus owner does not have the focuswriteMessage("The focus owner does not have the focus");}3.2 The GUI
4. Using Builder Classes
4.1 The Code
A simple example of the Builder class:
FxSceneExample3.java
package FXScene;import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.BorderPane;import javafx.scene.layout.BorderPaneBuilder;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.scene.shape.RectangleBuilder;import javafx.stage.Stage;public class FxSceneExample3 extends Application{public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the first RectangleRectangle rectangle1 = RectangleBuilder.create().x(10).y(20).width(200).height(100).fill(Color.RED).build();// Create a partially configured RectangleBuilder@SuppressWarnings("rawtypes")RectangleBuilder builder = RectangleBuilder.create().width(50).height(50).fill(Color.GREEN);// Create additional Rectangles using the BuilderRectangle rectangle2 = builder.x(250).y(50).build();Rectangle rectangle3 = builder.x(350).y(80).build();// Create the BorderPaneBorderPane root = BorderPaneBuilder.create().build();// Set the Size of the BorderPaneroot.setMinWidth(500);root.setMinHeight(200);// ASdd the Children to the Grouproot.getChildren().addAll(rectangle1, rectangle2, rectangle3);// Set the Style-properties of the BorderPaneroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("An example with Builder Classes");// Display the Stagestage.show();}}Using theSceneBuilder to create a wholeScene and all their children:
FxSceneExample4.java
package FXScene;import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.SceneBuilder;import javafx.scene.control.ButtonBuilder;import javafx.scene.control.LabelBuilder;import javafx.scene.layout.VBoxBuilder;import javafx.stage.Stage;public class FxSceneExample4 extends Application{public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the whole Scene using different SceneBuilder ClassesScene scene = SceneBuilder.create().width(300).height(100).root(VBoxBuilder.create().minWidth(300).minHeight(200).children(LabelBuilder.create().text("Hello Builder").build(),ButtonBuilder.create().text("Exit").onAction(new EventHandler(){@Overridepublic void handle(ActionEvent event){System.exit(0);}}).build()).build()).build();// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("Another example with Builder Classes");// Display the Stagestage.show();}}JavaFX provides two classes for creating and configuring objects that constitute the building blocks of a Scene Graph. One class is named after the type of object that the class represents. Another with the former class name suffixed with the word Builder. For example,Rectangle andRectangleBuilder classes exist to work with rectangles,Scene andSceneBuilder classes exist to work with scenes, and so on.
Builder classes provide three types of methods:
- They have a create() static method to create an instance of the builder class.
- They contain methods to set properties. Method names are the same as the property names that they set.
- They have a build() method that returns the object of the class for which the builder class exists. For example, the build() method of the RectangleBuilder class returns an object of the Rectangle class.
Builder classes are designed to use method chaining. Their methods to configure properties return the same builder instance.
The following snippet of code creates aRectangle, using theRectangle class, with (x, y) coordinates, with a width and a height. It also sets the fill property to red:
Rectangle r1 = new Rectangle(10, 20, 200, 100);r1.setFill(Color.RED);
You can use the RectangleBuilder class to create the same rectangle:
// Create the first RectangleRectangle rectangle1 = RectangleBuilder.create().x(10).y(20).width(200).height(100).fill(Color.RED).build();
Using builder classes requires longer code. However, it is more readable compared to using constructors to set the properties. Another advantage of builder classes is that they can be reused to build objects with slightly different properties. Suppose you want to create multiple rectangles with a 50px width and a 50px height, filled with the color red. However, they have different x and y coordinates.
You can do so with the following code:
// Create a partially configured RectangleBuilder@SuppressWarnings("rawtypes")RectangleBuilder builder = RectangleBuilder.create().width(50).height(50).fill(Color.GREEN);// Create additional Rectangles using the BuilderRectangle rectangle2 = builder.x(250).y(50).build();Rectangle rectangle3 = builder.x(350).y(80).build();4.2. The GUI
The following image shows the result of the first example:
The following image shows the result of the second one:

5. Understanding the Platform Class
5.1 The Code
FxSceneExample5.java
package FXScene;import javafx.application.Application;import javafx.application.Platform;import javafx.scene.Scene;import javafx.scene.control.TextArea;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class FxSceneExample5 extends Application{// Create the TextAreaTextArea textarea = new TextArea();public static void main(String[] args) {Application.launch(args);}@Overridepublic void init() {writeMessage("init(): " + Thread.currentThread().getName() + "\n");// Create a Runnable taskRunnable task = new Runnable(){public void run(){writeMessage("Running the task on the " + Thread.currentThread().getName());}};// Submit the task to be run on the JavaFX Application ThreadPlatform.runLater(task);}@Overridepublic void start(Stage stage) throws Exception{// Create the BorderPaneVBox root = new VBox();// Set the Style-properties of the VBOxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Add the TextArea to the Grouproot.getChildren().add(textarea);// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Platform Example");// Display the Stagestage.show();}private void writeMessage(String msg){textarea.appendText(msg + "\n");}}ThePlatform class in thejavafx.application package is a utility class used to support platform-related functionalities.

Thank you!
We will contact you soon.
TherunLater() method is used to submit aRunnable task to an event queue, so it is executed on the JavaFXApplication Thread. JavaFX allow developers to execute some of the code only on the JavaFXApplication Thread.
The following code creates a task in theinit() method that is called on the JavaFX Launcher Thread. It uses thePlatform.runLater() method to submit the task to be executed on the JavaFX Application Thread later:
@Overridepublic void init() {writeMessage("init(): " + Thread.currentThread().getName() + "\n");// Create a Runnable taskRunnable task = new Runnable(){public void run(){writeMessage("Running the task on the " + Thread.currentThread().getName());}};// Submit the task to be run on the JavaFX Application ThreadPlatform.runLater(task);}Some features in a JavaFX implementation are optional (or conditional). They may not be available on all platforms. Using an optional feature on a platform that does not support the feature does not result in an error; the optional feature is simply ignored. Optional features are defined as enum constants in theConditionalFeature enum in thejavafx.application package.
5.2. The GUI

6. Knowing the Host Environment
6.1 The Code
FxSceneExample6.java
package FXScene;import java.util.HashMap;import java.util.Map;import javafx.application.Application;import javafx.application.HostServices;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.control.TextArea;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.stage.Modality;import javafx.stage.Stage;import javafx.stage.StageStyle;import netscape.javascript.JSObject;public class FxSceneExample6 extends Application{// Create the TextAreaTextArea textarea = new TextArea();public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the URL-Stringfinal String yahooURL = "http://www.yahoo.com";// Create the Buttons and their EventHandlersButton openURLButton = new Button("Go to Yahoo!");openURLButton.setOnAction(new EventHandler<ActionEvent>(){@Overridepublic void handle(ActionEvent event){getHostServices().showDocument(yahooURL);}});Button showAlert = new Button("Show Alert");showAlert.setOnAction(new EventHandler<ActionEvent>(){@Overridepublic void handle(ActionEvent event){showAlert();}});// Create the HBoxHBox hbox = new HBox();// Add the Children to the HBoxhbox.getChildren().addAll(openURLButton, showAlert);// Create the VBoxVBox root = new VBox();// Set the width and height of the VBoxroot.setMinWidth(400);root.setMinHeight(300);// Add buttons and all host related details to the VBoxroot.getChildren().addAll(hbox,textarea);// Set the Style-properties of the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the MapMap<String, String> hostdetails = getHostDetails();// Add the Details to the Mapfor(Map.Entry<String, String> entry : hostdetails.entrySet()) {String desc = entry.getKey() + ": " + entry.getValue();textarea.appendText(desc + "\n");}// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("Knowing the Host");// Display the Stagestage.show();}protected Map<String, String> getHostDetails() {// Create the MapMap<String, String> map = new HashMap<>();// Create the HostHostServices host = this.getHostServices();// Get the Host DetailsString codeBase = host.getCodeBase();map.put("CodeBase", codeBase);String documentBase = host.getDocumentBase();map.put("DocumentBase", documentBase);JSObject js = host.getWebContext();map.put("Environment", js == null?"Non-Web":"Web");String splashImageURI = host.resolveURI(documentBase, "splash.jpg");map.put("Splash Image URI", splashImageURI);return map;}protected void showAlert() {// Create the HostHostServices host = getHostServices();// Create a JavaScriptObjectJSObject js = host.getWebContext();if (js == null) {// Create the StageStage stage = new Stage(StageStyle.UTILITY);stage.initModality(Modality.WINDOW_MODAL);// Create the LabelLabel label = new Label("This is an FX alert!");// create the VBoxVBox root = new VBox();// Set the width and height of the VBoxroot.setMinWidth(300);root.setMinHeight(200);// Set the Style-properties of the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Add the Children to the VBoxroot.getChildren().add(label);// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("FX Alert");//Display the Stagestage.show();}else {js.eval("window.alert('This is a JavaScript alert!')");}}}TheHostServices class in thejavafx.application package provides services related to the launching environment (desktop, web browser, or WebStart) hosting the JavaFX application. You cannot create an instance of theHostServices class directly. ThegetHostServices() method of theApplication class returns an instance of theHostServices class.
The following is an example of how to get an instance ofHostServices inside a class that inherits from theApplication class:
HostServices host = getHostServices();
TheHostServices class contains the following methods:
ThegetCodeBase() method returns the code base uniform resource identifier (URI) of the application. In a stand-alone mode, it returns the URI of the directory that contains the JAR file used to launch the application. If the application is launched using a class file, it returns an empty string. If the application is launched using a JNLP file, it returns the value for the specified code base parameter in the JNLP file.
ThegetDocumentBase() method returns the URI of the document base. In a web environment, it returns the URI of the web page that contains the application. If the application is launched usingWebStart, it returns the code base parameter specified in the JNLP file. It returns the URI of the current directory for application launched in stand-alone mode.
ThegetWebContext() method returns aJSObject that allows a JavaFX application to interact with the JavaScript objects in a web browser. If the application is not running in a web page, it returns null. You can use theeval() method of the JSObject to evaluate a JavaScript expression from inside your JavaFX code.
The following snippet of code displays an alert box using thewindow.alert() function. If the application runs in a nonweb environment, it shows a JavaFX modal stage instead:
// Create the HostHostServices host = getHostServices();// Create a JavaScriptObjectJSObject js = host.getWebContext();if (js == null) {// Create the StageStage stage = new Stage(StageStyle.UTILITY);stage.initModality(Modality.WINDOW_MODAL);// Create the LabelLabel label = new Label("This is an FX alert!");// create the VBoxVBox root = new VBox();// Set the width and height of the VBoxroot.setMinWidth(300);root.setMinHeight(200);// Set the Style-properties of the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Add the Children to the VBoxroot.getChildren().add(label);// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("FX Alert");//Display the Stagestage.show();}else {js.eval("window.alert('This is a JavaScript alert!')");}TheresolveURI() method resolves the specified relative URI with respect to the specified base URI and returns the resolved URI.
TheshowDocument() method opens the specified URI in a new browser window. Depending on the browser preference, it may open the URI in a new tab instead. This method can be used in a stand-alone mode as well as in a web environment.
The following snippet of code opens the Yahoo! home page:
getHostServices().showDocument("http://www.yahoo.com");6.2. The GUI
The following image shows a stage with two buttons and host details. One button opens the Yahoo! home page and another shows an alert box.

7. Download Java Source Code
This was an example ofjavafx.scene
You can download the full source code of this example here:JavaFxSceneExample.zip

Thank you!
We will contact you soon.






