22 February 2022

GUIs

Install and Test Download the Python program available here. It will autogenerate shell code for a JavaFX program.


/**************************************************
*   Author: Morrison
*   Date:  22 Feb 22
**************************************************/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class First extends Application
{
    @Override
    public void start(Stage primary)
    {

        primary.show();
    }

}

Application This manages the life cycle of a JavaFX application. It is the parent class of all JavaFX applictions. This class has one abstract method, void start(Stage primary).

Stage This is a top-level container for an application. An application can have several stages operating at once.

Scene This is a tree-like data structure that establishes a hierarchy among the visible elements on a page. The scene graph is a collection of scenes that can be shown in an app.

Node This is an element in a stage.

Parent Parents can contain other nodes in a scene.

Application Life Cycle.


/**************************************************
*   Author: Morrison
*   Date:  22 Feb 2022
**************************************************/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class LifeCycle extends Application
{
    public LifeCycle()

    {
        System.out.println("Constructor called");
    }

    @Override
    public void init()
    {
        System.out.println("init called");
    }

    @Override
    public void start(Stage primary)
    {
        System.out.println("start started");
        primary.show();
    }

    @Override
    public void stop()
    {
        System.out.println("stop called");
    }
}

Compile. Then run and you will see this.


$ java LifeCycle
Constructor called
init called
start started

Note that the system prompt does not return, as the child process you have spawned is now running. Click on the red go-away button and you will see this.


$ java LifeCycle
Constructor called
init called
start started
stop called

So, when a JavaFX program runs, things happen in this order.

  1. The contstructor is called; use this to initialize state.
  2. The init method is called. This can be used to set up housekeeping.
  3. The start method is the main event loop of our program. This runs until the user quits, or System.exit() is called.
  4. If the user quits via the go-away button or if he calls Platform.exit(), stop will be called. If System.exit is called, stop does not get called.
  5. Execution terminates