Java Reference
In-Depth Information
Although the skeleton is quite short, it can be compiled and run. It produces an empty
window. However, it also produces the following output on the console:
When you close the window, this message is displayed on the console:
Of course, in a real program, the life-cycle methods would not normally output anything
to System.out . They do so here simply to illustrate when each method is called. Further-
more, as explained earlier, you will need to override the init( ) and stop( ) methods only if
your application must perform special startup or shutdown actions. Otherwise, you can use
the default implementations of these methods provided by the Application class.
Let's examine this program in detail. It begins by importing four packages. The first is
javafx.application , which contains the Application class. The Scene class is packaged
in javafx.scene , and Stage is packaged in javafx.stage . The javafx.scene.layout package
provides several layout panes. The one used by the program is FlowPane .
Next, the application class JavaFXSkel is created. Notice that it extends Application .
As explained, Application is the class from which all JavaFX applications are derived.
JavaFXSkel contains four methods. The first is main( ) . It is used to launch the application
via a call to launch( ) . Notice that the args parameter to main( ) is passed to the launch( )
method. Although this is a common approach, you can pass a different set of parameters to
launch( ) , or none at all. One other point: launch( ) is required by a free-standing applica-
tion, but not in other cases. When it is not needed, main( ) is also not needed. However, for
reasons already explained, both main( ) and launch( ) are included in the programs in this
chapter.
When the application begins, the init( ) method is called first by the JavaFX run-time
system. For the sake of illustration, it simply displays a message on System.out , but it
would normally be used to initialize some aspect of the application. Of course, if no initial-
ization is required, it is not necessary to override init( ) because an empty, default imple-
mentation is provided. It is important to emphasize that init( ) cannot be used to create the
stage or scene portions of a GUI. Rather, these items should be constructed and displayed
by the start( ) method.
After init( ) finishes, the start( ) method executes. It is here that the initial scene is cre-
ated and set to the primary stage. Let's look at this method line-by-line. First, notice that
Search WWH ::




Custom Search