Updated March 23, 2023
Introduction to Java WindowListener
In Java, WindowListener class gets notified when there is a change in the window state. The window states can be close, activation, or deactivation of the window. WindowListener works with the help of implements keyword and this interface can be obtained from the package java.awt.event. More details on Java WindowListener will be explained in the following sections
Java WindowListener interface can be declared using the following syntax:
Public interface WindowListener extends EventListener
Methods of Java WindowListener
The following are the commonly used methods in Java WindowListener.
- windowActivated(WindowEvent ev): This method will be invoked when the window is set as active.
- windowDeiconified(WindowEvent ev): This method will be invoked when there is a change in the size of the window from minimal to normal state.
- windowClosing(WindowEvent ev): This method will be invoked when the user tries to get the window closed using the system menu in the window.
- windowClosed(WindowEvent ev): This method will be invoked when the window gets closed due to the calling of disposing in the window.
- windowDeactivated(WindowEvent ev): This method will be invoked when the window is not active anymore.
- windowOpened(WindowEvent e): This method will get invoked when a window is visible for the first time.
- windowIconified(WindowEvent e): This method will be invoked when there is a change in the size of the window from normal to a small state.
How Java WindowListener Works?
Here are some of the working of java window listener which are explained below:
- When there is a class that has to process certain window events, an object should be present in that which helps in implementing the interface.
- Since the object is already registered with the listener, an event will be generated on close, activate, iconify, deactivate, deiconify states of the window.
- This will help in the invocation of the relevant method in the object of the listener.
- After the invocation, the WindowEvent is passed to it.
Examples to Implement Java WindowListener
Below are some examples :
Example #1
Java program to demonstrate the WindowListener with texts displaying on console on certain window events.
Code:
//Java program to demonstrate the WindowListener with texts displaying on console on certain window events
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//class that implements the WindowListener interface
public class JWindowListenerExample implements WindowListener
{
//create a label
Label lbl1;
//create frame
Frame fr;
JWindowListenerExample()
{
fr = new Frame("Window events demo . . .");
lbl1= new Label(" Window events will be displayed on console", Label.CENTER);
//set the layout
fr.setLayout(new FlowLayout());
//add the label to the frame
fr.add(lbl1);
//In order to catch as well as respond to the mouse events, register the created class JWindowListenerExample
fr.addWindowListener(this);
//set the size of frame where width is 350 and height as 250
fr.setSize(350,250);
//set the visibility as true
fr.setVisible(true);
}
//implementation of windowActivated method
public void windowActivated(WindowEvent ev)
{
System.out.println("Window is activated now");
}
//implementation of windowClosed method
public void windowClosed(WindowEvent ev)
{
System.out.println("Now, the window is closed");
}
//implementation of windowClosing method
public void windowClosing(WindowEvent ev)
{
//release all the resources
fr.dispose();
System.out.println("Window is closing now");
}
//implementation of windowDeactivated method
public void windowDeactivated(WindowEvent ev)
{
System.out.println("Window gets Deactivated");
}
//implementation of windowDeiconified method
public void windowDeiconified(WindowEvent ev)
{
System.out.println("Window gets Deiconified");
}
//implementation of windowIconified method
public void windowIconified(WindowEvent ev)
{
System.out.println("Window gets minimized or iconified");
}
//implementation of windowOpened method
public void windowOpened(WindowEvent e)
{
System.out.println("The window is opening for the very first time");
}
//main method
public static void main(String... ar)
{
new JWindowListenerExample();
}
}
Output:
On executing the code, a window will appear with a label. Based on the events that happen in the window, certain texts will also be displayed in the console. First, let us see the window that appears immediately after executing the code.
It can be seen that the window is activated and opened for the first time. If the window is minimized, the console will show a text mentioning that the window is minimized and it is deactivated.
At the same time, if the window is deiconified, a text will display that the window is deiconified and activated.
Moreover, if the window is closed, the text will be shown such that the window is getting closed and deactivated.
Example #2
Java program to demonstrate the WindowListener without any texts on console on window events.
Code:
//Java program to demonstrate the WindowListener without any texts on console on window events
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//class that implements the WindowListener interface
public class JWindowListenerExample implements WindowListener
{
//create a label
Label lbl1;
//create frame
Frame fr;
JWindowListenerExample()
{
fr = new Frame("Window events demo . . .");
lbl1= new Label(" Window events will be displayed on console", Label.CENTER);
//set the layout
fr.setLayout(new FlowLayout());
//add the label to the frame
fr.add(lbl1);
//In order to catch as well as respond to the mouse events, register the created class JWindowListenerExample
fr.addWindowListener(this);
//set the size of frame where width is 350 and height as 250
fr.setSize(350,250);
//set the visibility as true
fr.setVisible(true);
}
//implementation of windowActivated method
public void windowActivated(WindowEvent ev)
{
}
//implementation of windowClosed method
public void windowClosed(WindowEvent ev)
{
}
//implementation of windowClosing method
public void windowClosing(WindowEvent ev)
{
fr.dispose();
}
//implementation of windowDeactivated method
public void windowDeactivated(WindowEvent ev)
{
}
//implementation of windowDeiconified method
public void windowDeiconified(WindowEvent ev)
{
}
//implementation of windowIconified method
public void windowIconified(WindowEvent ev)
{
}
//implementation of windowOpened method
public void windowOpened(WindowEvent e)
{
}
//main method
public static void main(String... ar)
{
new JWindowListenerExample();
}
}
Output:
It can be seen that a window will appear on executing the code. However, unlike the program 1, texts won’t be displayed on console during window events.
Conclusion
It is a class that gets notified when there is a change in the states of the window such as close, activation, or deactivation of the window. More details such as declaration, methods, working, and implementation of this interface are discussed in this document in detail.
Recommended Articles
This is a guide to Java WindowListener. Here we discuss the syntax, methods, and working of Java WindowListener along with examples and code implementation. You may also look at the following articles to learn more –