JavaFX
Basic Concepts
In general, the JavaFX
framework has all of the good features of Swing. For example, JavaFX is
lightweight. It can also support an MVC architecture. Much of what you already know
about creating GUIs using Swing is conceptually applicable to JavaFX. That
said, there are significant differences between the two.
From a programmer’s point of
view, the first differences you notice between JavaFX and Swing are the
organization of the framework and the relationship of the main components.
Simply put, JavaFX offers a more streamlined, easier-to-use, updated approach.
JavaFX also greatly simplifies the rendering of objects because it handles
repainting automatically. It is no longer necessary for your program to handle
this task manually. The preceding is not intended to imply that Swing is poorly
designed. It is not. It is just that the art and science of programming has
moved forward, and JavaFX has received the benefits of that evolution. Simply
put, JavaFX facilitates a more visually dynamic approach to GUIs.
The
JavaFX Packages
The JavaFX elements are
contained in packages that begin with the javafx
prefix. At the time of this writing, there are more than 30 JavaFX packages in
its API library. Here are four examples: javafx.application,
javafx.stage, javafx.scene, and javafx.scene.layout.
Although we will only use a
few of these packages in this chapter, you will want to spend some time
browsing their capabilities. JavaFX offers a wide array of functionality.
The
Stage and Scene Classes
The central metaphor
implemented by JavaFX is the stage.
As in the case of an actual stage play, a stage contains a scene. Thus, loosely speaking, a stage defines a space and a scene
defines what goes in that space. Or, put another way, a stage is a container
for scenes and a scene is a container for the items that comprise the scene. As
a result, all JavaFX applications have at least one stage and one scene. These
elements are encapsulated in the JavaFX API by the Stage and Scene classes.
To create a JavaFX application, you will, at minimum, add at least one Scene object to a Stage. Let’s look a bit more closely at these two classes.
Stage is a top-level container. All JavaFX applications automatically
have access to one Stage, called the primary
stage. The primary stage is supplied by the run-time system when a JavaFX application is started.
Although you can create other stages, for many applications, the primary stage
will be the only one required.
As mentioned, Scene is a container for the items that
comprise the scene. These can consist of controls, such as push buttons and
check boxes, text, and graphics. To create a scene, you will add those elements
to an instance of Scene.
Nodes
and Scene Graphs
The individual elements of a
scene are called nodes. For example,
a push button control is a node. However, nodes can also consist of groups of
nodes. Furthermore, a node can have a child node. In this case, a node with a
child is called a parent node or branch node. Nodes without children are
terminal nodes and are called leaves. The collection of all nodes in a scene
creates what is referred to as a scene
graph, which comprises a tree.
There is one special type of
node in the scene graph, called the root
node. This is the top-level node and is the only node in the scene graph
that does not have a parent. Thus, with the exception of the root node, all
other nodes have parents, and all nodes either directly or indirectly descend
from the root node.
The base class for all nodes
is Node. There are several other
classes that are, either directly or indirectly, subclasses of Node. These include Parent, Group, Region, and Control, to name a few.
Layouts
JavaFX provides several
layout panes that manage the process of placing elements in a scene. For
example, the FlowPane class provides
a flow layout and the GridPane class
supports a row/column grid-based layout. Several other layouts, such as BorderPane (which is similar to the
AWT’s BorderLayout), are available.
The layout panes are packaged in javafx.scene.layout.
The
Application Class and the Life-cycle Methods
A JavaFX application must be
a subclass of the Application class,
which is packaged in javafx.application.
Thus, your application class will extend
Application. The Application class
defines three life-cycle methods that your application can override. These are
called init( ), start( ), and stop( ),
and are shown here, in the order in which they are called:
void init( )
abstract void start(Stage primaryStage) void stop( )
The init( ) method is called when the application begins execution. It
is used to perform various initializations. As will be explained, however, it cannot be used to create a stage or
build a scene. If no initializations are required, this method need not be
overridden because an empty, default version is provided.
The start( ) method is called after init( ). This is where your application begins and it can be used to construct and set the
scene. Notice that it is passed a reference to a Stage object. This is the stage provided by
the run-time system and is the primary stage. (You can also create other
stages, but you won’t need to for simple applications.) Notice that this method
is abstract. Thus, it must be overridden by your application.
When your application is terminated,
the stop( ) method is called. It is
here that you can handle any cleanup or shutdown chores. In cases in which no
such actions are needed, an empty, default version is provided.
Launching
a JavaFX Application
To start a free-standing
JavaFX application, you must call the launch(
) method defined by Application.
It has two forms. Here is the one used in this chapter:
public static void
launch(String ... args)
Here, args is a possibly empty list of strings that typically specify
command-line arguments. When called, launch(
) causes the application to be constructed, followed by calls to init( ) and start( ). The launch( )
method will not return until after the application has terminated. This version
of launch( ) starts the subclass of Application from which launch( ) is called. The second form of
launch( ) lets you specify a class
other than the enclosing class to start.
Before moving on, it is
necessary to make an important point: JavaFX applications that have been
packaged by using the javafxpackager
tool (or its equivalent in an IDE) do not need to include a call to launch( ). However, its inclusion often
simplifies the test/debug cycle, and it lets the program be used without the
creation of a JAR file. Thus, it is included in all of the JavaFX programs in
this book.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.