Java WindowListener Interface

The WindowListener interface in Java provides methods for handling events that occur when a window is opened, closed, activated, deactivated, and more.

To use the WindowListener interface, you must implement its methods in your class and then register the listener with a window using the addWindowListener() method of the Window class.

Here is a list of the methods in the WindowListener interface:

  1. public void windowOpened(WindowEvent e) – Invoked when the window is first opened.
  2. public void windowClosing(WindowEvent e) – Invoked when the user attempts to close the window by clicking on the “X” button.
  3. public void windowClosed(WindowEvent e) – Invoked when the window has been closed.
  4. public void windowIconified(WindowEvent e) – Invoked when the window is minimized.
  5. public void windowDeiconified(WindowEvent e) – Invoked when the window is restored from being minimized.
  6. public void windowActivated(WindowEvent e) – Invoked when the window is activated.
  7. public void windowDeactivated(WindowEvent e) – Invoked when the window is deactivated.

To use these methods, you must override them in your class and provide the code to be executed when each event occurs.

For example, here’s how you could create a window and add a WindowListener to it:

import java.awt.*;
import java.awt.event.*;

public class MyWindow extends Frame implements WindowListener {
    
    public MyWindow() {
        setTitle("My Window");
        setSize(400, 400);
        setLocationRelativeTo(null);
        addWindowListener(this);
    }
    
    // Implement the methods of the WindowListener interface
    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) { System.exit(0); }
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    
    public static void main(String[] args) {
        MyWindow window = new MyWindow();
        window.setVisible(true);
    }
}

In this example, the windowClosing() method has been overridden to exit the program when the user clicks the “X” button to close the window.

WindowListener interface declaration:

Here is the declaration of the WindowListener interface in Java:

public interface WindowListener extends EventListener {
    
    public void windowOpened(WindowEvent e);
    public void windowClosing(WindowEvent e);
    public void windowClosed(WindowEvent e);
    public void windowIconified(WindowEvent e);
    public void windowDeiconified(WindowEvent e);
    public void windowActivated(WindowEvent e);
    public void windowDeactivated(WindowEvent e);
    
}

The WindowListener interface extends the EventListener interface, which is the root interface for all listener interfaces in the Java API. The WindowListener interface declares seven methods, each corresponding to a different event that can occur in a window.

Each method takes a single parameter of type WindowEvent, which contains information about the event that occurred. When a window event occurs, the appropriate method in the listener object is invoked by the system, passing a WindowEvent object to it. The listener object can then perform the necessary actions in response to the event.

Methods of WindowListener interface:

The WindowListener interface in Java defines seven methods, each corresponding to a different event that can occur in a window. Here are the methods of the WindowListener interface:

  1. public void windowOpened(WindowEvent e) – Invoked when the window is first opened.
  2. public void windowClosing(WindowEvent e) – Invoked when the user attempts to close the window by clicking on the “X” button.
  3. public void windowClosed(WindowEvent e) – Invoked when the window has been closed.
  4. public void windowIconified(WindowEvent e) – Invoked when the window is minimized.
  5. public void windowDeiconified(WindowEvent e) – Invoked when the window is restored from being minimized.
  6. public void windowActivated(WindowEvent e) – Invoked when the window is activated.
  7. public void windowDeactivated(WindowEvent e) – Invoked when the window is deactivated.

To use these methods, you must implement them in your class and provide the code to be executed when each event occurs. When a window event occurs, the appropriate method in the listener object is invoked by the system, passing a WindowEvent object to it. The listener object can then perform the necessary actions in response to the event.

Methods inherited by the WindowListener:

The WindowListener interface in Java extends the EventListener interface and does not define any additional methods. However, the EventListener interface is a marker interface, which means it does not have any methods of its own. Instead, it is used to mark an interface as being an event listener interface.

So the WindowListener interface does not inherit any methods from the EventListener interface. However, any class that implements the WindowListener interface must provide implementations for all of the methods defined in the interface. Otherwise, the class must be declared as abstract.

It’s also worth noting that there are several other interfaces in the Java AWT and Swing APIs that extend the WindowListener interface, such as WindowStateListener, WindowFocusListener, and WindowAdapter. These interfaces provide additional methods for handling window events, and they can be used in conjunction with the WindowListener interface to provide more comprehensive event handling.

Working of WindowListener interface:

The WindowListener interface in Java is used to handle window events such as opening, closing, iconifying, and deiconifying a window. When a window event occurs, the appropriate method in the listener object is invoked by the system, passing a WindowEvent object to it. The listener object can then perform the necessary actions in response to the event.

Here is an example of how to use the WindowListener interface in Java:

import java.awt.*;
import java.awt.event.*;

public class MyWindow extends Frame implements WindowListener {

    public MyWindow() {
        // set the properties of the window
        setTitle("My Window");
        setSize(400, 400);
        setLocationRelativeTo(null);
        
        // add the window listener to the window
        addWindowListener(this);
    }

    // implement the methods of the WindowListener interface
    public void windowOpened(WindowEvent e) {
        System.out.println("Window opened");
    }

    public void windowClosing(WindowEvent e) {
        System.out.println("Window closing");
        dispose(); // dispose of the window
        System.exit(0); // exit the program
    }

    public void windowClosed(WindowEvent e) {
        System.out.println("Window closed");
    }

    public void windowIconified(WindowEvent e) {
        System.out.println("Window iconified");
    }

    public void windowDeiconified(WindowEvent e) {
        System.out.println("Window deiconified");
    }

    public void windowActivated(WindowEvent e) {
        System.out.println("Window activated");
    }

    public void windowDeactivated(WindowEvent e) {
        System.out.println("Window deactivated");
    }

    public static void main(String[] args) {
        MyWindow window = new MyWindow();
        window.setVisible(true);
    }
}

In this example, the MyWindow class extends the Frame class and implements the WindowListener interface. The addWindowListener() method is used to register the MyWindow object as a listener for window events.

The methods of the WindowListener interface are then implemented in the MyWindow class. Each method prints a message to the console indicating which window event occurred.

When the user clicks on the “X” button to close the window, the windowClosing() method is invoked. This method disposes of the window using the dispose() method and exits the program using the System.exit() method.

This is a basic example of how to use the WindowListener interface in Java. By implementing the methods of the WindowListener interface, you can respond to various window events and perform the necessary actions in your program.

Java WindowListener Example:

Sure, here’s an example of how to use the WindowListener interface in Java to create a simple window that listens for events:

import java.awt.*;
import java.awt.event.*;

public class MyWindow extends Frame implements WindowListener {
    
    public MyWindow() {
        super("My Window");
        setSize(400, 300);
        setLocationRelativeTo(null);
        addWindowListener(this);
    }

    public void windowOpened(WindowEvent e) {
        System.out.println("Window opened");
    }

    public void windowClosing(WindowEvent e) {
        System.out.println("Window closing");
        dispose();
        System.exit(0);
    }

    public void windowClosed(WindowEvent e) {
        System.out.println("Window closed");
    }

    public void windowIconified(WindowEvent e) {
        System.out.println("Window iconified");
    }

    public void windowDeiconified(WindowEvent e) {
        System.out.println("Window deiconified");
    }

    public void windowActivated(WindowEvent e) {
        System.out.println("Window activated");
    }

    public void windowDeactivated(WindowEvent e) {
        System.out.println("Window deactivated");
    }

    public static void main(String[] args) {
        MyWindow window = new MyWindow();
        window.setVisible(true);
    }
}

In this example, we create a new class MyWindow that extends the Frame class and implements the WindowListener interface. The constructor of MyWindow sets the title and size of the window, sets the location to the center of the screen using setLocationRelativeTo(null), and adds the WindowListener to the window using addWindowListener(this).

The methods of the WindowListener interface are then implemented in the MyWindow class. In this example, each method simply prints a message to the console indicating which window event occurred.

Finally, we create an instance of MyWindow and make it visible using setVisible(true). This creates a new window with the title “My Window” that responds to various window events. When the user clicks on the “X” button to close the window, the windowClosing() method is invoked, which disposes of the window and exits the program.

You can run this code to see the output messages printed to the console as you interact with the window.