**JavaFX** # JavaFX Overview JavaFX is a collection of packages that can be used to create programs with a graphical user interface. ## Application Class To create a JavaFX application we create a class that extends the [Application](http://javadoc.taylorial.com/javafx.graphics/application/Application.html) class. The class contains an abstract method, [start()](http://javadoc.taylorial.com/javafx.graphics/application/Application.html#start%28javafx.stage.Stage%29), which must be overridden. The `start()` method must be passed the primary stage which defines the UI for the application. The `init()` and `stop()` may be overridden to provide initial set-up and tear-down instructions. The implementations in the `Application` are empty. When an instance of the subclass of `Application` in instantiated, `init()` is called first, then `start(Stage stage)` is called. When the application is finished (either with an explicit call to `Platform.exit()` (preferred over `System.exit()`) or when the last window is closed (provided `Platform.implicitExit==true`)), `stop()`. is called. ## Stage Class The [Stage](http://javadoc.taylorial.com/javafx.graphics/stage/Stage.html) class is a JavaFX container that describes the UI for the application. The primary `Stage` object is created by the JVM and passed to the `Application.start()` method. A minimal JavaFX application is shown below: ~~~~ Java public class HelloWorld extends Application { @Override public void start(Stage stage) { Label message = new Label("Hello World!"); Scene scene = new Scene(message); stage.setTitle("Title bar"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } } ~~~~ We need to import the appropriate `javafx.` statements (be sure to select the `javafx` packages and not other ones). In the above code, the `start()` method creates a `Label` object and adds it to a `Scene`. The scene of the stage is then set to this newly created scene. ## Scene Class The `Scene` class is the container for the graphical user interface content. The content is represented as a tree of **nodes** (called a scene graph) where each node is either a **branch** or a **leaf**. When instantiated, the `Scene` is passed the root node of the tree. ## Panes In the very simple example above we placed a control (`message`) directly onto the scene. In non-trivial GUI applications we will make use of `Pane`s to organize the layout of our controls in the window. There are a number of types of panes available for us to use, e.g., `VBox`, `HBox`, `FlowPane`, and `GridPane`. ## Controls The `Control` class is the base class for all user interface controls. A control is a node in the scene graph which can be manipulated by the user. Since `Control` is a subclass of `Region`, they can be styled with CSS (Cascading Style Sheets). The `Label` class is a specific type of control. A label is a non-editable text control that can be used to display text. There are many other types of controls available to use in JavaFX. For example, [Button](http://javadoc.taylorial.com/javafx.controls/scene/control/Button.html), [TextField](http://javadoc.taylorial.com/javafx.controls/scene/control/TextField.html), [ScrollPane](http://javadoc.taylorial.com/javafx.controls/scene/control/ScrollPane.html), [ListView](http://javadoc.taylorial.com/javafx.controls/scene/control/ListView.html), and [many others](http://javadoc.taylorial.com/javafx.controls/scene/control/package-summary.html).