CS 346 (W23)
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Classes

In JavaFX, our highest level abstractions are the Application class, with one or more Stage classes representing the application windows, and one or more Scene classes to manage the contents of a window. Nodes represent the individual graphical elements.

JavaFX Stage, Scene and Nodes

As we saw in the previous chapter with JavaFX, it’s standard practice in 2D graphical applications to represent the interface as a scene graph of objects. In JavaFX, the Scene class maintains this scene graph, consisting of various nodes, for each Scene that we display. Note that it’s possible to have multiple windows, each with multiple scenes, each of which manages a different scene graph. (Multiple windows can be displayed at once, but only once scene graph can be displayed at a given time in a window, representing the current window contents).

Application

The Application class is top-level representation of the application. It serves as the application entry point, replacing the main() method. During launch, a JavaFX application will perform the followin steps:

  1. Constructs an instance of the specified Application class
  2. Calls the init() method
  3. Calls the start(javafx.stage.Stage) method (passing in a default stage)
  4. Waits for the application to finish, which happens when either of the following occur:
    • the application calls Platform.exit()
    • the last window has been closed and the implicitExit attribute on Platform is true
  5. Calls the stop() method

The start() method is abstract and MUST be overridden. The init() and stop() methods are optional, but MAY be overridden. It’s fairly normal to just override start() and ignore the others most of the time.

Stage

The Stage class is the top-level container or application window. You can have multiple stages, representing multiple windows.

 javafx.stage.Window
    javafx.stage.Stage

A Stage instance is automatically created by the runtime, and passed into the start() method.

Stage methods operate at the window level:

  • setMinWidth(), setMaxWidth()
  • setResizable()
  • setTitle()
  • setScene()
  • show()

Scene

The Scene is a container for the content in a scene-graph. Although you can create multiple scenes, only one can be attached to a window at a time, representing the “current” contents of that window.

javafx.scene.Scene

To construct a scene, and set it up:

  • Create a scene graph consisting of a container holding on or more nodes;
  • Add the root node of the scene graph to the scene;
  • Add the scene to a stage and make the stage visible.

Scene methods manipulate the scene graph, or attempt to set properties for the entire graph:

  • setRoot(Node)
  • setFill(Paint)
  • getX(), getY()

Node

Node is the base class for all elements of a scene graph. Types of nodes include:

Nodes have common properties for position (x, y), width and height, background colour and so on. These can be set manually in code, or in the case of visual properties, associated with a CSS stylesheet.

JavaFX is pretty complete, but you might want to implement something that isn’t built into that toolkit e.g. date widgets.

Luckily, you can include projects that expand the standard widgets. These are intended to be imported and used alongside the standard JavaFX widgets.

  • ControlsFX expands to include checklists, breadcrumb bars and other unique widgets.
  • JFxtras includes a calendar widget, gauges and other useful widgets.