Java Reference
In-Depth Information
Builders were very popular in JavaFX. However, it turned out that there were major technical hurdles with
keeping them in the platform. As a consequence, it has been decided to phase builders out. In Java 8, Builder classes
are still usable, but they are deprecated. In Java 9, Builder classes might be removed entirely.
More information on the reason why Builder classes are not preferred anymore can be found in a mailing list
entry by JavaFX Chief Architect Richard Bair at http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-
March/006725.html . The bottom of this entry contains a very important statement: “I believe that FXML or Lambda's
or alternative languages all provide other avenues for achieving the same goals as builders but without the additional
cost in byte codes or classes.”
This is what we will show throughout this topic. Near the end of this chapter, we show a first example of a Lambda
expression in our code. In Chapter 3, we show how SceneBuilder and FXML allow you to use a declarative way of
defining a UI.
In the current example, we programmatically define the different components of the UI, and we glue them
together. In Chapter 3, we show the same example using a declarative FXML-based approach.
The JavaFX Application
Let's have a look at the class declaration in our first example:
public class HelloEarthRiseMain extends Application
This declaration states that our application extends the javafx.application.Application class. This class has
one abstract method that we should implement:
public void start(Stage stage) {}
This method will be called by the environment that executes our JavaFX application.
Depending on the environment, JavaFX applications will be launched in a different way. As a developer, you
don't have to worry about how your application is launched, and where the connection to a physical screen is made.
You have to implement the “start” method and use the provided Stage parameter to create your UI, as discussed in
the next paragraph.
In our command-line example, we launched the applications by executing the main method of the application
class. The implementation of the main method is very simple:
public static void main(String[] args) {
Application.launch(args);
}
The only instruction in this main method is a call to the static launch method of the application, which will
launch the application.
Tip
a JavaFX application always has to extend the javafx.application.Application class.
A Stage and a Scene
A Stage contains the UI of a JavaFX app, whether it is deployed on the desktop, on an embedded system, or on other
devices. On the desktop, for example, a Stage has its own top-level window, which typically includes a border and
title bar.
 
Search WWH ::




Custom Search