by Dinesh Thakur Category: Swing

Window event occurs when window related activities such as closing, activating or deactivating a window are performed. Objects representing window events are created from WindowEvent class. The most common method of this class is

Window getWindow()

It returns the Window object that generated the event.

There are three listener interfaces corresponding to the WindowEvent class. These include WindowListener interface, WindowFocusListener interface and WindowStateListener interface. Each listener for WindowEvent should implement the appropriate interface. 


The methods of this interface are:



windowOpened (WindowEvent e)

Invoked when the window opens.

windowClosing (WindowEvent e)

Invoked when trying to close the window.

windowClosed (WindowEvent e)

Invoked when the window has been permanently closed.

windowIconified (WindowEvent e)

Invoked when the window is minimized.

windowDeiconified (WindowEvent e)

Invoked when the window goes from being minimized to be normal.

windowActivated (WindowEvent e)

Invoked when the window becomes the active window.

windowDeactivated (WindowEvent e)

Invoked when the Window is no longer the active window.

windowClosing () is called when you click on system closing the window box. WindowClosed () is called after closing the window: This method is only useful if the closure of the window does not cause the end of application.

import javax.swing.*;     

import java.awt.event.*;
import java.awt.*;
class WindowListenerExample extends JFrame implements WindowListener
       public void windowClosing(WindowEvent e)
          System.out.println("Window Closing");
       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");

WindowListener in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles