Java WindowListener

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

Methods of WindowListener interface

The signature of 7 methods found in WindowListener interface is given below:

  1. public abstract void windowActivated(WindowEvent e);  
  2. public abstract void windowClosed(WindowEvent e);  
  3. public abstract void windowClosing(WindowEvent e);  
  4. public abstract void windowDeactivated(WindowEvent e);  
  5. public abstract void windowDeiconified(WindowEvent e);  
  6. public abstract void windowIconified(WindowEvent e);  
  7. public abstract void windowOpened(WindowEvent e);  

Java WindowListener Example

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame implements WindowListener{  
  5.     WindowExample(){  
  6.         addWindowListener(this);  
  7.           
  8.         setSize(400,400);  
  9.         setLayout(null);  
  10.         setVisible(true);  
  11.     }  
  12.       
  13. public static void main(String[] args) {  
  14.     new WindowExample();  
  15. }  
  16. public void windowActivated(WindowEvent arg0) {  
  17.     System.out.println("activated");  
  18. }  
  19. public void windowClosed(WindowEvent arg0) {  
  20.     System.out.println("closed");  
  21. }  
  22. public void windowClosing(WindowEvent arg0) {  
  23.     System.out.println("closing");  
  24.     dispose();  
  25. }  
  26. public void windowDeactivated(WindowEvent arg0) {  
  27.     System.out.println("deactivated");  
  28. }  
  29. public void windowDeiconified(WindowEvent arg0) {  
  30.     System.out.println("deiconified");  
  31. }  
  32. public void windowIconified(WindowEvent arg0) {  
  33.     System.out.println("iconified");  
  34. }  
  35. public void windowOpened(WindowEvent arg0) {  
  36.     System.out.println("opened");  
  37. }  
  38. }  

Output:

java awt windowlistener example 1

java awt windowlistener example 2