Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

Window event in JavaFX | 100% Best for beginners

December 5, 2021
in Java
Reading Time: 4 mins read
0
Window Event in JavaFX
1k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • JavaFX Tutorial
  • Window Event in JavaFX
    • How to use the window close event in Java
    • Examples to use the Window Event in Java
      • Output
    • Example: Window close event in Java
      • Output
  • YouTube Video

JavaFX Tutorial

Window event in JavaFX is part of the javafx.stage package in JavaFX. If you are just getting started with Java programming, you should also learn about window events. In Java, the window event contains four constants: WINDOW_SHOWING, WINDOW_SHOWN, WINDOW_HIDING, WINDOW_HIDDEN, and WINDOW_CLOSE_REQUEST.

Window Event in Java

The following event types and descriptions can be found listed below. Please keep reading to find gain more knowledge in window events, or click here to learn more about Java programming.

Window Event in JavaFX

The WindowEvent class in JavaFX is easy to understand and the constants in the WindowEvent class in JavaFX are listed below.

  • WINDOW_SHOWING – This type of event will occur just before the window is shown.
  • WINDOW_SHOWN – This type of event will occur just after the window is shown.
  • WINDOW_HIDING – This type of event will occur just before the window is hidden.
  • WINDOW_HIDDEN – This type of event will occur just after the window is hidden.
  • WINDOW_CLOSE_REQUEST – This type of event will occur if the developer generated the window close event in Java to close the window.

The WINDOW_SHOWING and WINDOW_SHOWN events are quick. This events will be generated just before and after the window is shown. If you use the WINDOW_SHOWING event, it might delay the displaying of the window to the user and degrading to the user experience.

Window Event in Java

The WINDOW_SHOWN event is also useful since it indicates the user’s beginning point, such as presenting an alert to attract the user’s attention or focusing the textfield if you want the textfield to be the first move of the application. However, It is totally up to you on how you use the WINDOW_SHOWN event in JavaFX. The opposite of the WINDOW_SHOWING and WINDOW_SHOWN events are the WINDOW_HIDING and WINDOW_HIDDEN events. These events will be generated before and after the window is hidden.

How to use the window close event in Java

To use the window close event in Java or  the WINDOW_CLOSE_REQUEST. The event occurs when there is an external request to close the window. It is considered an external request to close the window if the user clicks the close icon in the window or presses the Alt + F4 key.  Closing the window programmatically, such as using the stage’s close() method or the Platform.exit() method, is not considered as an external request. The windlow will not close if the WINDOW_CLOSE_REQUEST is consumed.

Examples to use the Window Event in Java

In this example, I will demonstrate how to use the window events and show you how the window events works.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST;

/**
 *
 * @author KENSOFT
 */
public class WindowEvt extends Application {
    
    private CheckBox cbx = new CheckBox("Can close the window");
    
    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        root.getChildren().add(cbx);
        
        primaryStage.setOnCloseRequest(event ->{
            event.consume();
            closeRequest(primaryStage);
            handler(event);
        });
        
        primaryStage.setOnHidden(event ->{
            handler(event);
        });
        
        primaryStage.setOnHiding(event ->{
            handler(event);
        });
        
        primaryStage.setOnShowing(event ->{
            handler(event);
        });
        
        primaryStage.setOnShown(event ->{
            handler(event);
        });
        
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Window Event Tutorial");
        primaryStage.show();
    }
    
    // A window event handler to check if we can close the application
    private void handler(WindowEvent evt){
        EventType<WindowEvent> window = evt.getEventType();
        // If the checkbox is not selected, the WINDOW_CLOSE_REQUEST will consume.
        // it means that you can't close the window.
        if(window == WINDOW_CLOSE_REQUEST && !cbx.isSelected()){
            evt.consume();
        }
        System.out.println("Event:" +evt.getEventType()+" Consumed: "+evt.isConsumed());
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output

Event:WINDOW_SHOWING Consumed: false
Event:WINDOW_SHOWN Consumed: false
Event:WINDOW_CLOSE_REQUEST Consumed: false
Event:WINDOW_HIDING Consumed: false
Event:WINDOW_HIDDEN Consumed: false

Example: Window close event in Java

In this example, I will show you how to use the WINDOW_CLOSE_REQUEST event and alerting the user for the confirmation to close the window.

// method to close the window
private void closeRequest(Stage stage){
    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setTitle("Close the window");
    alert.setContentText("Are you sure you want close the application?");
    
    if(alert.showAndWait().get() == ButtonType.OK){
        Platform.exit();
    }
}

// Set on close request
primaryStage.setOnCloseRequest(event ->{
     event.consume();
     closeRequest(primaryStage);
});

Output

Window Close Event in Java

YouTube Video

YouTube video

Tags: Window Close event in JavaWindow Event in JavaWindow event in JavaFX
Previous Post

How to use the Key Event in JavaFX | 100% Best for beginners

Next Post

How to use Java Jsoup Tutorial | 100% perfect for beginners

KENSOFT

KENSOFT

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
212
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
220
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
648
Next Post
Java Jsoup Tutorial

How to use Java Jsoup Tutorial | 100% perfect for beginners

Comments in Java

Comments in Java | 100% Perfect for beginner

Read file in Java

How to read file in Java | 100% Perfect for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025
top 10 common coding mistakes beginner make

Top 10 Common Coding Mistakes Beginners Make

June 29, 2025
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025

© 2025 Made With Love By KENSOFT PH

No Result
View All Result
  • Blog
    • Java
    • Programming Tips
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2025 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.