Java FX Intro
FXML applications
Java FX contains a number of libraries with pre-built components,
containers, and layouts -- just like the Swing libraries do. However, a
more common way to put together the components and layouts is using an
associated .fxml file, which sets component properties using a
form of XML (Extended Markup Language).
FXML layouts can be created easily with the Scene Builder tool, which
will create the same FXML file set-up regardless of IDE used. This tool
is similar to the idea of a WYSYWIG ("what you see is what you get")
editor for web pages.
The JavaFX Application Window
A JavaFX application window consists of:
- Controls: These are the GUI components, like text fields,
buttons, etc. Similar to the components in the Swing libraries
- Stage: The window in which the JavaFX app's GUI is displayed.
class Stage is in the package javafx.stage
- Scene: The stage contains an active scene, defining
the GUI as a scene graph -- this is a tree data structure of the
visual elements on the app. class Scene is in package
javafx.scene
- Nodes: visual elements in the scene graph. The root
node is the outermost, typically a container that contains other
elements (nodes) inside. Each other node has one parent
- Layout containers: A node that has children is typically a
layout container that arranges the child nodes in a scene.
Containers can contain other containers!
- Event Handler and Controller Class: A GUI event handler in an
FXML GUI is defined in a controller class -- i.e. the class where
the event handlers go.
The Application class
- JavaFX applications extend from class Application, in the
package javafx.application
- A JavaFX application can be launched from a main program by calling
the static method launch(String... args). (Note that this
allows a main() function's commmand-line parameters to be sent along
into the applciation
- When a JavaFX application is launched:
- An instance of the specified Application class is created (an
object)
- The init() method is called for that object
- The start(Stage) method is called for that object
- The application finishes either when Platform.exit() is
called, or when the last window has been closed and
the implicitExit attribute on Platform is
true
- The stop() method is called for the object
- Note that this structure is similar to that of an applet, which has
four functions used for its core control (init, start, stop,
destroy)
- init(), start(), and stop() are all
inherited methods that can be overridden. start() is the only
one that must be overridden, because it is an abstract
method
Small example JavaFX
application class
Format of an application that uses a .fxml file for its GUI
layout
While we can use Scene Builder to create the .fxml file that
specifies the components, properties, and layouts of GUI elements, we
still must put it into an application. Here is a sample outline:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class YaddaYadda extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root =
FXMLLoader.load(getClass().getResource("yadda.fxml"));
Scene scene = new Scene(root); // attach scene graph to scene
scene.setTitle("Yadda App"); // set the title bar of the window
stage.setScene(scene); // attach scene to the stage
stage.show(); // display the stage
}
public static void main(String[] args)
{
launch(args); // create a YaddaYadda object and call start
}
}
Note that the main() function simply launches an instance of
the application -- in this case, the class that extends
Application.
Also note that the .fxml file with the GUI items and layout
is loaded by the static load() method of class
FXMLLoader.
Scene Builder user guide
We will do some demos in class on building some GUI layouts with
SceneBuilder