Java AWT Tutorial- Java WindowListener Interface

The Java WindowListener is notified whenever you change the state of window. It is notified against WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods.

WindowListener interface declaration

The declaration for java.awt.event.WindowListener interface is shown below:

public interface WindowListener extends EventListener  

Methods of WindowListener interface

The signature of 7 methods found in WindowListener interface with their usage are given below:

Sr. no. Method signature Description
1. public abstract void windowActivated (WindowEvent e); It is called when the Window is set to be an active Window.
2. public abstract void windowClosed (WindowEvent e); It is called when a window has been closed as the result of calling dispose on the window.
3. public abstract void windowClosing (WindowEvent e); It is called when the user attempts to close the window from the system menu of the window.
4. public abstract void windowDeactivated (WindowEvent e); It is called when a Window is not an active Window anymore.
5. public abstract void windowDeiconified (WindowEvent e); It is called when a window is changed from a minimized to a normal state.
6. public abstract void windowIconified (WindowEvent e); It is called when a window is changed from a normal to a minimized state.
7. public abstract void windowOpened (WindowEvent e); It is called when window is made visible for the first time.

Methods inherited by the WindowListener

This interface inherits methods from the EventListener interface.

Working of WindowListener interface

If a class needs to process some Window events, an object should exist which can implement the interface.

As the object is already registered with Listener, an event will be generated on all the states of window.

This helps in generation of invocation of relevant method in listener’s object. And then WindowEvent is passed after invocation.

Java WindowListener Example

In the following example, we are going to implement all the method of WindowListener interface one by one.

WindowExample.java

// importing necessary libraries of awt  
import java.awt.*;    
import java.awt.event.WindowEvent;    
import java.awt.event.WindowListener;   
  
// class which inherits Frame class and implements WindowListener interface   
public class WindowExample extends Frame implements WindowListener {    
  
// class constructor  
    WindowExample() {   
  
    // adding WindowListener to the frame   
        addWindowListener(this);   
        // setting the size, layout and visibility of frame        
        setSize (400, 400);    
        setLayout (null);    
        setVisible (true);    
    }   
// main method  
public static void main(String[] args) {    
    new WindowExample();    
}    
  
// overriding windowActivated() method of WindowListener interface which prints the given string when window is set to be active  
public void windowActivated (WindowEvent arg0) {    
    System.out.println("activated");    
}    
  
// overriding windowClosed() method of WindowListener interface which prints the given string when window is closed  
public void windowClosed (WindowEvent arg0) {    
    System.out.println("closed");    
}    
  
// overriding windowClosing() method of WindowListener interface which prints the given string when we attempt to close window from system menu  
public void windowClosing (WindowEvent arg0) {    
    System.out.println("closing");    
    dispose();    
}    
  
// overriding windowDeactivated() method of WindowListener interface which prints the given string when window is not active  
public void windowDeactivated (WindowEvent arg0) {    
    System.out.println("deactivated");    
}    
  
// overriding windowDeiconified() method of WindowListener interface which prints the given string when window is modified from minimized to normal state  
public void windowDeiconified (WindowEvent arg0) {    
    System.out.println("deiconified");    
}    
  
// overriding windowIconified() method of WindowListener interface which prints the given string when window is modified from normal to minimized state  
public void windowIconified(WindowEvent arg0) {    
    System.out.println("iconified");    
}    
  
// overriding windowOpened() method of WindowListener interface which prints the given string when window is first opened  
public void windowOpened(WindowEvent arg0) {    
    System.out.println("opened");    
}    
}    

Output:

If you like this answer, you can give me a coffee by click here (view Ads)