GUIs
Install and Test Download the Python program available here. It will autogenerate shell code for a JavaFX program.
- Get the Liberica JDK
- On Windoze, it does not install by default. Note Paul's Canvas announcement.
- The program below should compile and it should show you a little window.
- This might be useful for Windoaze
/**************************************************
* 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.
- The contstructor is called; use this to initialize state.
- The
init
method is called. This can be used to set up housekeeping. - The
start
method is the main event loop of our program. This runs until the user quits, orSystem.exit()
is called. - If the user quits via the go-away button or
if he calls
Platform.exit()
, stop will be called. IfSystem.exit
is called,stop
does not get called. - Execution terminates