The class which processes the WindowEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addWindowListener() method.

Interface declaration

Following is the declaration for java.awt.event.WindowListener interface:

  1. public interface WindowListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void windowActivated(WindowEvent e) Invoked when the Window is set to be the active Window.
2 void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window.
3 void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu.
4 void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window.
5 void windowDeiconified(WindowEvent e) Invoked when a window is changed from a minimized to a normal state.
6 void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state.
7 void windowOpened(WindowEvent e) Invoked the first time a window is made visible.

Methods inherited

This interface inherits methods from the following interfaces:

  • java.awt.EventListener

WindowListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showWindowListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showWindowListenerDemo(){
  47. headerLabel.setText("Listener in action: WindowListener");
  48.  
  49. Button okButton = new Button("OK");
  50.  
  51. aboutFrame = new Frame();
  52. aboutFrame.setSize(300,200);;
  53. aboutFrame.setTitle("WindowListener Demo");
  54. aboutFrame.addWindowListener(new CustomWindowListener());
  55.  
  56. Label msgLabel = new Label("Welcome to tutorialspoint.");
  57. msgLabel.setAlignment(Label.CENTER);
  58. msgLabel.setSize(100,100);
  59. aboutFrame.add(msgLabel);
  60. aboutFrame.setVisible(true);
  61. }
  62.  
  63. class CustomWindowListener implements WindowListener {
  64. public void windowOpened(WindowEvent e) {
  65. }
  66.  
  67. public void windowClosing(WindowEvent e) {
  68. aboutFrame.dispose();
  69. }
  70.  
  71. public void windowClosed(WindowEvent e) {
  72. }
  73.  
  74. public void windowIconified(WindowEvent e) {
  75. }
  76.  
  77. public void windowDeiconified(WindowEvent e) {
  78. }
  79.  
  80. public void windowActivated(WindowEvent e) {
  81. }
  82.  
  83. public void windowDeactivated(WindowEvent e) {
  84. }
  85. }
  86. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_windowlistener.jpg