Open In App

Java WindowListener in AWT

The Abstract Window Toolkit (AWT) of Java provides a collection of graphical user interface (GUI) components for creating desktop applications. When it comes to managing window-related events, the WindowListener interface is used. The WindowListener interface provides methods for responding to window-related events such as opening, closing, activating or deactivating, and more such methods.

WindowListener in AWT

The WindowListener interface in Java AWT is imported from the java.awt.event package. It is the basic connection or a link between your application and the window’s actions. By implementing this interface and overriding its methods in your class, you can respond to a wide range of window events.



Syntax of WindowListener:

public interface WindowListener extends EventListener

The WindowListener interface in Java is declared with the above statement. It extends the EventListener interface which indicates that it helps in capturing and reacting to different window events in Java applications.

WindowListener Interface Methods

Given below are the abstract methods of the WindowListener interface.



Method

Modifier Type

Description

windowActivated​(WindowEvent e)

void

This method is called when the window is configured to be the active window.

windowClosed​(WindowEvent e)

void

Triggered upon the closing of a window as an outcome of calling dispose on the window.

window closing​(WindowEvent e)

void

Invoked whenever a user tries to dismiss a window via the system menu.

windowDeactivated​(WindowEvent e)

void

Called when a window isn’t the active window anymore.

windowDeiconified​(WindowEvent e)

void

Called when the conversion of a window from its minimized to its regular state.

windowIconified (WindowEvent e)

void

Triggered when there is a transition of a window from its normal minimized state.

windowOpened​(WindowEvent e)

void

It is called the first time a window appears.

Note: Methods of the WindowListener interface are inherited from the EventListener interface.

Example of Java WindowListener

Given below is a simple example to demonstrate the working of the WindowListener interface.




// Java program to demonstrate the use of WindowListener
// interface and its methods
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowListener;
  
// Driver CLass
public class Window implements WindowListener {
    public Window()
    {
        // Create a frame
        Frame f = new Frame("WindowListener Example");
  
        // Create a label
        Label l = new Label("GeeksforGeeks");
  
        // Set properties of label
        l.setBounds(100, 90, 140, 20);
        l.setForeground(Color.GREEN);
        l.setFont(new Font("Serif", Font.BOLD, 22));
  
        // Add it to the frame
        f.add(l);
  
        // Add windowListener to the frame
        f.addWindowListener(this);
  
        // Set properties of frame
        f.setSize(400, 300);
        f.setLayout(null);
        f.setVisible(true);
    }
  
    // Override all the abstract methods of WindowListener
    // interface
    public void windowOpened(WindowEvent e)
    {
        System.out.println("Window is opened!");
    }
  
    public void windowClosing(WindowEvent e)
    {
        System.out.println("Window is closing...");
        System.exit(0);
    }
  
    public void windowClosed(WindowEvent e)
    {
        System.out.println("Window is closed!");
    }
  
    public void windowIconified(WindowEvent e)
    {
        System.out.println("Window is iconified!");
    }
  
    public void windowDeiconified(WindowEvent e)
    {
        System.out.println("Window is deiconified!");
    }
  
    public void windowActivated(WindowEvent e)
    {
        System.out.println("Window is activated!");
    }
  
    public void windowDeactivated(WindowEvent e)
    {
        System.out.println("Window is deactivated!");
    }
  
    // Main method
    public static void main(String[] args) { new Window(); }
}

Use below commands to run the program,

javac Window.java
java Window


Output of the above code in GIF:

Explanation of the example:

Applications of the WindowListener Interface

There are many use cases of WindowListener interface, which we can use in our Java application to perform different actions.

  1. Confirmation on Exit: Before application get terminated, you may ask the user for confirmation by using the windowClosing method.
  2. Saving Data: As the window closes, you may want to save any unsaved configurations or data.
  3. Dynamic UI Updates: When the window is enabled or deactivated, certain actions, such as UI updating or data refreshing, can be initiated.
  4. Media Player Interactions: When a user navigates to another window or program in a media player, the windowDeactivated event can be used to trigger an automated halt in playing.
  5. Status Updates: The windowDeactivated event in a chat application might be used to have the user’s status changed to “Away” or “Inactive” if they go to another program.

Article Tags :