Window Event in Java – Kensoft PH https://kensoftph.com Power up your knowledge in programming Sun, 05 Dec 2021 11:43:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://kensoftph.com/wp-content/uploads/2021/07/cropped-Kensoftph-New-logo-32x32.png Window Event in Java – Kensoft PH https://kensoftph.com 32 32 Window event in JavaFX | 100% Best for beginners https://kensoftph.com/how-to-use-the-window-event-in-javafx/ https://kensoftph.com/how-to-use-the-window-event-in-javafx/#respond Sun, 05 Dec 2021 11:43:45 +0000 https://kensoftph.com/?p=1778 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

]]>
https://kensoftph.com/how-to-use-the-window-event-in-javafx/feed/ 0 JavaFX Tutorial: Window Event in Java For Beginners nonadult