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.
WindowListener interface declaration
The declaration for java.awt.event.WindowListener interface is shown below:
Methods of WindowListener interface
The signature of 7 methods found in WindowListener interface with their usage are given below:
||public abstract void windowActivated (WindowEvent e);
||It is called when the Window is set to be an active Window.
||public abstract void windowClosed (WindowEvent e);
||It is called when a window has been closed as the result of calling dispose on the window.
||public abstract void windowClosing (WindowEvent e);
||It is called when the user attempts to close the window from the system menu of the window.
||public abstract void windowDeactivated (WindowEvent e);
||It is called when a Window is not an active Window anymore.
||public abstract void windowDeiconified (WindowEvent e);
||It is called when a window is changed from a minimized to a normal state.
||public abstract void windowIconified (WindowEvent e);
||It is called when a window is changed from a normal to a minimized state.
||public abstract void windowOpened (WindowEvent e);
||It is called when window is made visible for the first time.
Methods inherited by the WindowListener
This interface inherits methods from the EventListener interface.
Working of WindowListener interface
- 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 Listener, an event will be generated on all the states of window.
- This helps in generation of invocation of relevant method in listener's object. And then WindowEvent is passed after invocation.
Java WindowListener Example
In the following example, we are going to implement all the method of WindowListener interface one by one.