Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java windowListener Interface
2.1.
Java WindowListener interface declaration
3.
Methods of Java WindowListener interface
4.
The Working of the WindowListener interface
5.
Java WindowListener Examples
5.1.
Example 1
6.
Frequently Asked Questions
6.1.
What is the use of WindowListener?
6.2.
How many methods are there in the WindowListener interface?
6.3.
What is event and listener in Java?
6.4.
What is an interface listener?
6.5.
From where does the WindowListener interface inherits its methods? 
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Java Window Listener

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

Also Read About, Multithreading in java

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

Methods of Java WindowListener interface

The following table displays the various methods that are present in the Java WindowListener interface.

Method

Description

windowOpened (WindowEvent e) When the Window is set to be an active Window, this method is called.
windowClosing (WindowEvent e) This method is invoked when we have to close the window.
windowClosed (WindowEvent e) This method is invoked when the window has been closed permanently.
windowIconified (WindowEvent e) This method is used when the window has to be minimized.
windowDeiconified (WindowEvent e) This method is used when the window becomes normal from being minimized.
windowActivated (WindowEvent e) This method is invoked when the window becomes an active window.
windowDeactivated (WindowEvent e) This method is called when the Window no longer remains the active window.

You can also read about the Multiple Inheritance in Java and Hashcode Method in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

The Working of the WindowListener interface

The Java WindowListener interface works in the following way:

  • 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 the listener, an event will be generated on all the states of the window.
  • This helps in the generation of invocation of appropriate method in listener's object. And then WindowEvent is passed after invocation.

Read More About, Basics of Java

Java WindowListener Examples

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);
    }
}

Output 1

Output of Java WindowListener

 

Practice by yourself on online java compiler for better understanding.

Frequently Asked Questions

What is the use of WindowListener?

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.

After reading about the Java windowListener, are you not feeling excited to read/explore more articles on Java? Don't worry; Coding Ninjas has you covered. To learn, see  Basics of Java with Data Structures and AlgorithmsJava foundation for freeJava learn and practiceUnderstanding Java packages listJava AWT tutorial, and Difference between AWT and Swing in Java.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion

Live masterclass