/**
  * Because there may be many AppContexts, and we can't be sure where this EmbeddedFrame is first
  * created or shown, we can't automatically determine the correct KeyboardFocusManager to attach
  * to as KeyEventDispatcher. Those who want to use the functionality of traversing out of the
  * EmbeddedFrame must call this method on the Applet's AppContext. After that, all the changes can
  * be handled automatically, including possible replacement of KeyboardFocusManager.
  */
 public void registerListeners() {
   if (appletKFM != null) {
     removeTraversingOutListeners(appletKFM);
   }
   appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
   if (isVisible()) {
     addTraversingOutListeners(appletKFM);
   }
 }
  /**
   * Needed to track which KeyboardFocusManager is current. We want to avoid memory leaks, so when
   * KFM stops being current, we remove ourselves as listeners.
   */
  public void propertyChange(PropertyChangeEvent evt) {
    // We don't handle any other properties. Skip it.
    if (!evt.getPropertyName().equals("managingFocus")) {
      return;
    }

    // We only do it if it stops being current. Technically, we should
    // never get an event about KFM starting being current.
    if (evt.getNewValue() == Boolean.TRUE) {
      return;
    }

    // should be the same as appletKFM
    removeTraversingOutListeners((KeyboardFocusManager) evt.getSource());

    appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (isVisible()) {
      addTraversingOutListeners(appletKFM);
    }
  }
 /**
  * Needed to avoid memory leak: we register this EmbeddedFrame as a listener with
  * KeyboardFocusManager of applet's AppContext. We don't want the KFM to keep reference to our
  * EmbeddedFrame forever if the Frame is no longer in use, so we add listeners in show() and
  * remove them in hide().
  */
 public void hide() {
   if (appletKFM != null) {
     removeTraversingOutListeners(appletKFM);
   }
   super.hide();
 }