Window Listener Event in Advanced Java Programming

Home  »  Java  »  Advance Java »    Java Event Handling »    WindowListener

The WindowListener interface defines seven methods for handling window events. An application of the WindowListener interface is to close a Frame window. The seven methods of the WindowListener interface are:

  1. void windowActivated(WindowEvent we) Is invoked when a window is activated.
  2. void windowDeactivated(WindowEvent we) Is invoked when a window is deactived.
  3. void windowOpened(WindowEvent we) Is invoked when a window is opened.
  4. void windowClosed(WindowEvent we) Is invoked when a window is closed.
  5. void windowClosing(WindowEvent we) Is invoked when a window is closing
  6. void windowIconified(WindowEvent we) Is invoked when a window minimized.
  7. void windowDeiconfied(WindowEvent we) Is invoked when a window is restored.

The addWindowListener() method enables monitoring a window for the occurrence of a window event. The following program code shows how to implement the WindowListener interface:

Implementing the WindowListener Interface:


The above code shows closing a Frame window using the WindowListener interface. the windowClosing() method uses the setVisible method to close the frame window. The System.exit(0) method stop the program from runing and code will be stored in the file called WindowListenerTest.java, which on compilation will crate a class file called WindowListenerTest.class. Now compile this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac WindowListenerTest.java
C:\>jdk1.4\bin>java WindowListenerTest


"Output of WindowListenerTest.java"


Download Complete Program

  -  



Event Listener


Action Listener Adjustment Listener Mouse Listener
Mouse Motion Listener Window Listener Key Listener
Focus Listener ITEM Listener Text Listener



1 comment: