Java Reference
In-Depth Information
5
6 public class MyJavaFX extends Application {
7 @Override // Override the start method in the Application class
8 public void start(Stage primaryStage) {
9 // Create a scene and place a button in the scene
10 Button btOK = new Button( " OK " );
11 Scene scene = new Scene(btOK, 200 , 250 );
12 primaryStage.setTitle( " MyJavaFX " ); // Set the stage title
13 primaryStage.setScene(scene); // Place the scene in the stage
14
extend Application
override start
create a button
create a scene
set stage title
set a scene
display stage
primaryStage.show(); // Display the stage
15 }
16
17 /**
18 * The main method is only needed for the IDE with limited
19 * JavaFX support. Not needed for running from the command line.
20 */
21 public static void main(String[] args) {
22 Application.launch(args);
23 }
24 }
main method
launch application
You can test and run your program from a command window or from an IDE such as
NetBeans or Eclipse. A sample run of the program is shown in Figure  14.1. Supplements
II.F-H give the tips for running JavaFX programs from a command window, NetBeans, and
Eclipse. A JavaFX program can run stand-alone or from a Web browser. For running a JavaFX
program from a Web browser, see Supplement II.I.
JavaFX on NetBenas and
Eclipse
F IGURE 14.1
A simple JavaFX displays a button in the window.
The launch method (line 22) is a static method defined in the Application class for
launching a stand-alone JavaFX application. The main method (lines 21-23) is not needed if
you run the program from the command line. It may be needed to launch a JavaFX program
from an IDE with a limited JavaFX support. When you run a JavaFX application without a
main method, JVM automatically invokes the launch method to run the application.
The main class overrides the start method defined in javafx.application.Application
(line 8). After a JavaFX application is launched, the JVM constructs an instance of the class
using its no-arg constructor and invokes its start method. The start method normally
places UI controls in a scene and displays the scene in a stage, as shown in Figure 14.2a.
Line 10 creates a Button object and places it in a Scene object (line 11). A Scene object
can be created using the constructor Scene(node, width, height) . This constructor
specifies the width and height of the scene and places the node in the scene.
A Stage object is a window. A Stage object called primary stage is automatically cre-
ated by the JVM when the application is launched. Line 13 sets the scene to the primary stage
and line 14 displays the primary stage. JavaFX names the Stage and Scene classes using the
analogy from the theater. You may think stage as the platform to support scenes and nodes as
actors to perform in the scenes.
You can create additional stages if needed. The JavaFX program in Listing 14.2 displays
two stages, as shown in Figure 14.2b.
launch
construct application
start application
scene
primary stage
 
Search WWH ::




Custom Search