Simple JavaFX Example

Purpose

The purpose of this guide is to provide a simple example of using JavaFX in your Aussom application. From the JavaFX Website "JavaFX is an open source, next generation client application platform for desktop, mobile and embedded systems built on Java." That pretty much sums it up. JavaFX is an awesome cross-platform UI toolkit and it's available to use in Aussom! Read on to get started.

The Application

This is a minimal "Hello, World" application but it illustrates how easy it is to get started with JavaFX and build beautiful GUI applications with Aussom.

include aji;
include fx;
include fx.StackPane;
include fx.Label;
class HelloFX {
    public main(args) {
        app = fx.fxApp("HelloFX!", 200, 100);
        layout = new StackPane();
        layout.add(new Label("Hello, Aussom FX!"));
        app.setLayout(layout.obj);
        app.show(true);
        fx.shutdown();
    }
}

To try out this application copy the code above and save it as hellofx.aus. You can then run it from the CLI with this command.

$ aussom hellofx.aus

If all goes well you should see a window that looks something like this.

JavaFX Example Window Image

Description

Let's go over each part here, especially if you're new to JavaFX. To start with we need to import the JavaFX modules we want to use. We need to always include the main JavaFX module fx, and then after that we include the modules of the JavaFX components we are using. For a list see the Aussom API Reference section of the Documentation Page.

Note that in this 1.0 release of Aussom, you also need to import aji as it's used by the JavaFX implementation. Future versions won't require this include though as it will be automatically included from the fx module.

include aji;
include fx;
include fx.StackPane;
include fx.Label;

Next in the main() function we create a new fxApp object with the following fx function call. In this case it will produce a new JavaFX appliation with the provided title string and the window width and height dimensions.

app = fx.fxApp("HelloFX!", 200, 100);

We then create a new JavaFX StackPane object stored in the layout variable. We create a new JavaFX Label and add it to the layout.

layout = new StackPane();
layout.add(new Label("Hello, Aussom FX!"));

We set the StackPane layout we just created as the main layout for the application window by using the fxApp.setLayout() function.

Note that in the 1.0 Aussom release, we have to make sure to pass the StackPane AussomJavaObjectj (layout.obj) to the setLayout function. This will change in the next release and going forward, you will just be able to call app.setLayout(layout) instead. Both ways will be acceptable in future releases so this method will still work.

app.setLayout(layout.obj);

Finally we call app.show(true) to show the window. The true bool value passed tells the function to block the main thread while this window is open. You can leave it off and just use app.show() if you like because the argument defaults to true. Lastly we need to tell JavaFX we are completely done with the application, and we call fx.shutdown() to accomplish this.

app.show(true);
fx.shutdown();