How to Write a Focus Listener
by, 04-26-2012 at 09:30 PM (490 Views)
When keyboard focus is being gained or lost by the component, focus events usually get fired. This is applicable no matter change in focus would be occurring via keyboard, mouse or programmatically.
Here is will be explained that how focus events could be obtained for a component by making a FocusListener instance registered at it. Only for window, focus can be obtained by implementation of the WindowFocusListener instance. For many components implementation of the PropertyChangeListener instance could be considered at the class KeyboardFocusManager.
Focus events could be demonstrated by the given example. Many components are being displayed by the window. Focus listener which is registered at every component will be reporting each focus lost or gained event. For every event, other components present in the focus change, opposite Component gets reported. For instance, when focus is shifted from button towards the text field, button would be firing the focus lost event and text field would be firing the focus gained event. Focus gained as well as lost events could not be permanent. Let us say for example, temporary lost event would occur as the focus is lost by the window. A temporary events would be occurring at popup menus.