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.
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).
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:
- Constructs an instance of the specified Application class
- Calls the
init()
method - Calls the
start(javafx.stage.Stage)
method (passing in a default stage) - 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 onPlatform
is true
- the application calls
- 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.
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()
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
is the base class for all elements of a scene graph. Types of nodes include:
- a drawing Canvas and drawable shapes like Circle, Rectangle, and Line.
- standard widgets like Button, MenuBar, Spinner, Label and TextField.
- media playback widgets like ImageView and MediaView.
- animations like SequentialTransition and FadeTransition.
- meta objects like Camera and LightBase to offer fine control of the scene.
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.