What\'s the main difference between the method windowActivated (implemented from WindowListener) and windowGainedFocus (implemented from WindowFocusListener)?
The Java d
The focused window is the one that receives keyboard input. An active window is typically the document window that the user is manipulating. An active window is generally distinguished visually, for example, with a different title bar.
In macOS, the focused window is called the key window and the active window (there can be only one) is called the main window.
The distinction is subtle because they are almost always the same window.
An example where they differ would be a floating palette that contains a text field. The palette would need to be the focused window to accept keyboard input, but the document window is the active window where the changes are actually made and it should be distinguished from other (inactive) document windows.
Although Java distinguishes active and focused windows in its API, the implementation links them together so that some combinations (like the above example) are not possible, or at least difficult to arrange. For example, if you click on a focusable Java window, it becomes both the focused window and the active window.
From How to Write Window Listeners which reflects the quote in your question aswell:
windowActivated(WindowEvent)
andwindowDeactivated(WindowEvent)
:Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, the windowGainedFocus and windowLostFocus methods to determine when a window gains or loses the focus are preferred.
So windowActivated
is only executed when the window is a frame or dialog, while the windowGainedFocus
is for all types.