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.

Through whatever means it gets it, 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 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 e)
  public abstract void focusLost(FocusEvent e)

Previous | Next | Top
Last Modified March 16, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@sunsite.unc.edu