Focus Events

At any given time exactly one component in an applet should have the focus. The component that possesses the focus receives user input from the keyboard and the mouse. In other words it is the component to which low-level events are directed.

The focus can be adjusted in a number of ways. For example, if the user hits the tab key, the focus will generally shift from one component to the next. If the user hits Shift-Tab, the focus will move back one component. When the user selects a component with the mouse, that component gets the focus.

When a component gains or loses the focus, it gets a FocusEvent. A change in the focus can be permanent or temporary. Permanent focus change occurs when the focus is directly moved from one component to another, either by calling requestFocus() or by direct user action such as the Tab key. Temporary focus change occurs when a component gains or loses focus as an indirect result of another operation, such as a window deactivation. In this case, the original focus state is automatically restored once the operation is finished, or the window is reactivated.

This is a bad choice of terms. Even a component that has the focus permanently can lose it.

The isTemporary() method returns true if the focus change is temporary, false if it's permanent.

public boolean isTemporary()

You respond to changes in focus by installing a FocusListener in the component. This interface declares two methods:

  public abstract void focusGained(FocusEvent evt)
  public abstract void focusLost(FocusEvent evt)

Previous | Next | Top | Cafe au Lait

Copyright 1997, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified April 26, 2006