JavaFX Terminate Application - Quit Button Example

In this source code, we will show you how to stop or terminate the JavaFX application.

JavaFX Quit Button Example - Terminate Application

In the following example, we have a Button control. When we click on the button, the application terminates. When a button is pressed and released, an ActionEvent is sent.

package com.javaguides.javafx;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class QuitButtonExample extends Application {

    public void start(Stage stage) {

        Button btn = new Button();
        btn.setOnAction((ActionEvent event) -> {

        HBox root = new HBox();
        root.setPadding(new Insets(25));

        Scene scene = new Scene(root, 300, 220);

        stage.setTitle("Quit button");

    public static void main(String[] args) {


Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course