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:

The Application class

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