Window event in Java
A window event in Java is a low-level event that indicates that a window has changed its status. For example, a window event can be generated when a window is opened, closed, activated, deactivated, or when focus is transferred into or out of the Window.
Window event class
The object of this class represents the change in the state of a window. This low-level event is generated by a Window object when it is opened, closed, activated, deactivated, etc.
Class constructors
- WindowEvent (Window source, int id)
Constructs a WindowEvent object.
This method throws an IllegalArgumentException if the source is null.
- WindowEvent (Window source, int id, int oldState, int newState)
Creates a WindowEvent object with the provided new window state and previous window state.
If the source is null, then it throws an IllegalArgumentException.
- WindowEvent (Window source, int id, Window opposite)
creates a WindowEvent object with the specified opposite Window. The other window engaged in this focus or activation change is the one on its opposite side. This is the Window that lost activation or focus in the event of a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event. This is the Window that obtained activation or focus in the case of a WINDOW_DEACTIVATED or WINDOW_LOST_FOCUS event. The opposite Window is null if this focus transition takes place with a native application, with a Java application in a separate VM, or with no other Window.
If the source is null, this method throws an IllegalArgumentException.
- WindowEvent (Window source, int id, Window opposite, int oldState, int newState)
Constructs a WindowEvent object.
If the source is null, then it throws an IllegalArgumentException.
Class methods
- int getNewState ()
Returns the window's new state for WINDOW_STATE_CHANGED events. A bitwise mask is used to represent the state.
- int getOldState ()
The previous state of the window is returned for WINDOW_STATE_CHANGED events.
- Window getOppositeWindow ()
Returns the other Window involved in this focus or activation change. This is the Window that lost activation or focus in the event of a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event.
- Window getWindow ()
Returns the originator of the event.
- String paramString ()
A parameter string indicating this event is returned. This approach is helpful for both debugging and event logging.
Fields
- public static final int WINDOW_ACTIVATED
A type of window-activated event. When the window is selected as the active window, this event is sent. The only Windows that can be active are Frames or Dialogs. The native windowing system may use unique decorations, like a highlighted title bar, to identify the active Window or its children. The first Frame or Dialog that is the owner of the focused Window, or the focused Window itself, is always the active Window.
- public static final int WINDOW_DEACTIVATED
The window-deactivated event type. When the window is no longer the active window, this event is sent. The only Windows that can be active are Frames or Dialogs. The native windowing system may use unique decorations, like a highlighted title bar, to identify the active Window or its children. The first Frame or Dialog that is the owner of the focused Window, or the focused Window itself, is always the active Window.
- public static final int WINDOW_OPENED
The window opened the event. This event is sent only the first time a window is made visible.
- public static final int WINDOW_CLOSED
The window closed event. This event is sent after the displayable window has been closed as the result of a call to destroy.
- public static final int WINDOW_CLOSING
The "window is closing" event. When the user tries to close the window using the window's system menu, this event is sent. The window-closing procedure will be stopped if the software does not explicitly hide the window when handling this event.
- public static final int WINDOW_ICONIFIED
The iconified window event. This event is sent when the window changes from its default state to its minimized state. The icon supplied in the window's iconImage attribute is displayed as the minimized window on many systems.
- public static final int WINDOW_DEICONIFIED
The window deiconified event type. When the window changes from a minimized to a regular state, this event occurs.
- public static final int WINDOW_FIRST
The first number in the range of IDs is used for window events.
- public static final int WINDOW_GAINED_FOCUS
The window-gained-focus event class. This event is sent when the window is selected as the focused window, which causes the window or one of its components to start receiving keyboard events.
- public static final int WINDOW_LOST_FOCUS
The event type for Windows needs to be more focused. When a window is no longer the focused window, this event is sent, which prevents keyboard events from being sent to the window or any of its components.
- public static final int WINDOW_STATE_CHANGED
The event type of the window state changing. This event is sent whenever a window's status changes due to its iconification, maximization, etc.
- public static final int WINDOW_LAST
For window events, the last ID in the list is used.
WindowListener Interface:
The listener interface for receiving window events. Either this interface—and all of its methods—is implemented by the class that is interested in handling window events, or the class extends the abstract WindowAdapter class—overriding only the relevant methods. The window's addWindowListener function is then used to add the listener object made from that class to the window. The appropriate method in the listener object is called, and the WindowEvent is passed to it when the status of the window changes because of being opened, closed, active or deactivated, or iconified or deiconified.
Let us see the Java program to handle the window events.
Filename: WindowListenerExample.java
import java.awt.event.*; import javax.swing.JFrame; public class WindowListenerExample implements WindowListener { public void windowActivated(WindowEvent e) { System.out.println("Window activated"); } public void windowClosing(WindowEvent e) { System.out.println("Window closing"); } public void windowClosed(WindowEvent e) { System.out.println("Window closed"); } public void windowDeactivated(WindowEvent e) { System.out.println("Window deactivated"); } public void windowDeiconified(WindowEvent e) { System.out.println("Window deiconified"); } public void windowIconified(WindowEvent e) { System.out.println("Window iconified"); } public void windowOpened(WindowEvent e) { System.out.println("Window opened"); } public static void main(String[] args) { WindowListenerExample example = new WindowListenerExample(); JFrame frame = new JFrame(); frame.addWindowListener(example); frame.setSize(300, 200); frame.setVisible(true); } }
Output:
Window activated Window opened Window deactivated Window activated Window iconified Window deactivated Window closing
Explanation:
Window is opened:
When the frame is made visible, the windowOpened method is called, and it prints "Window opened" to the console.
Window is activated:
When the frame gains focus or becomes the active window, the windowActivated method is called, and it prints "Window activated" to the console.
Window is closed:
If you close the frame (click the close button), the windowClosing method is called, and it prints "Window closing" to the console.
This is followed by the windowClosed method being called, and it prints "Window closed" to the console.
Other Window Events:
The remaining methods (windowDeactivated, windowDeiconified, windowIconified) will be triggered based on user interactions (such as minimizing, maximizing, or switching focus).