LifeCycle_ClassEventHandling
  • JavaFX runtime creates several threads. At different stages in the application, threads are used to perform different tasks.
  • The JavaFX runtime creates,two threads, among other threads,
    JavaFX-Launcher
    JavaFX Application Thread
  • The launch() method of the Application class, is called only once; and it invokes the following threads.
  • JavaFX runtime calls the following methods of the specified JavaFX Application class in order:
    The no-args constructor
    The init() method : JavaFX Launcher Thread calls the init().; this thread can't have a Stage or Scene
    The start() method:: This thread contains Stage and Scene
    The stop() method
  • EventHandler in  a class;
  • Platform.exit()
Code :

package javafxtemplate1;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
*
* @author Manas9
*/
public class JavaFXTemplate1 extends Application {
public String str1;
@Override
public void init() {
str1 = Thread.currentThread().getName();
//System.out.println("init() method: " + name);
}
@Override
public void start(Stage primaryStage) {
Group groot = new Group();
Button btn1 = new Button("Title Button1");
Button btn2 = new Button("Exit Platform");
btn1.setLayoutX(100);btn1.setLayoutY(80);
Label label1 = new Label("Label");
label1.setLayoutX(90);label1.setLayoutY(40);
//Lambda expression  class
btn1.setOnAction((ActionEvent e) -> {
// System.out.println(str1+ "Hello World");
label1.setText(str1);//
primaryStage.setTitle("Hello World");
});
btn2.setOnAction(new DemoEventHandler1(){ });
groot.getChildren().addAll(btn1, label1, btn2);
Scene scene = new Scene(groot, 200, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class DemoEventHandler1 implements EventHandler<ActionEvent>{

@Override
public void handle(ActionEvent event) {
Platform.exit();

}
}

Runtime Views: (Click on Exit Platform to close the display stage.

Title Button

Exit Platform will close the application.