Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
  • 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
690
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

  • 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

JavaFX Tutorial: Window Event in Java For Beginners
Watch this video on YouTube.

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

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to use the JavaFX ToolBar
Java

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
13
JavaFX Context Menu
Java

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
10
JavaFX MenuBar
Java

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023
76
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 *

Maximum upload file size: 20 MB. You can upload: image, audio, video, document, text. Drop files here

  • Trending
  • Comments
  • Latest
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
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
JavaFX 17

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

November 15, 2021 - Updated on December 13, 2021
change the stage icon in JavaFX

How to change the stage icon in JavaFX | 100% best for beginners

May 23, 2021 - Updated on September 24, 2022
Failed to automatically set up a JavaFX Platform

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

send SMS in java

How to send SMS in Java | 100% best example

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

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023
How to use the Slider in JavaFX | 100% Perfect Tutorial

How to use the Slider in JavaFX | 100% Perfect Tutorial

November 27, 2022

Latest Tutorials

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023

Popular Tutorials

  • Failed to automatically set up a JavaFX Platform

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

    0 shares
    Share 0 Tweet 0
  • How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Website

Privacy Policy

Terms and Condition

Sitemap

Services

Guest Post

Fiverr

Freelancer

Upwork

Categories

Website Status

Check the status

Latest Tutorials

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023

© 2023 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
  • Java Quiz
  • Contact
  • About

© 2023 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.