Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
When window-related actions like closing, activating, or deactivating a window are initiated, a window event is triggered. WindowEvent class is used to generate objects that represent window events.
In the following section, we will dive deeper into the Java windowListener Interface.
When we alter the state of a window, the Java WindowListener gets informed. It is also notified when a WindowEvent occurs. The java.awt.event package contains the WindowListener interface.
Java WindowListener interface declaration
The java.awt.event.WindowListener interface is declared in the following manner as shown below:
public interface WindowListener extends EventListener
You can also try this code with Online Java Compiler
The following code displays the use of several methods present in the WindowListener Interface like windowClosing, windowOpened, windowClosed, windowActivated, wimdowDeavtivated, windowIconified, and windowDeiconified. All these methods are called from the main function by passing the appropriate arguments in them.
Example 1
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class WindowListenerExample extends JFrame implements WindowListener
{
WindowListenerExample()
{
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.out.println("Window Closing");
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e)
{
System.out.println("Window Open");
}
public void windowClosed(WindowEvent e)
{
System.out.println("Window Closed");
}
public void windowActivated(WindowEvent e)
{
System.out.println("Window Activated");
}
public void windowDeactivated(WindowEvent e)
{
System.out.println("Window Deactivated");
}
public void windowIconified(WindowEvent e)
{
System.out.println("window Iconified");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("Window Deiconified");
}
}
class WindowListenerJavaExample
{
public static void main(String[] args)
{
WindowListenerExample frame = new WindowListenerExample();
frame.setTitle("Window Listener Java Example");
frame.setBounds(100,200,200,200);
frame.setVisible(true);
}
}
You can also try this code with Online Java Compiler
Custom window-closing behavior is frequently implemented using window listeners. A window listener, for example, can be used to save data before shutting a window or to quit the application when the final window closes.
How many methods are there in the WindowListener interface?
Three listener interfaces correspond to the WindowEvent class. These include the WindowListener interface, WindowFocusListener interface, and WindowStateListener interface.
What is event and listener in Java?
In Java, an event listener is a component that "listens" for an event, such as a mouse click or a key press, and then responds appropriately. A listener for an event must be linked to an event object that defines the event.
What is an interface listener?
The Event listener represents the interfaces responsible for handling events. Java includes several Event listener classes. Every event listener method takes a single argument in the form of an object that is a subclass of the EventObject class.
From where does the WindowListener interface inherits its methods?
The WindowListener interface inherits methods from the EventListener interface.
Conclusion
In this article, we have extensively discussed the Java windowListener. We began with a brief introduction to windowListener, followed by its various methods and key coding examples.