title: what causes component requestFocus to fail sometimes?
body: <p>I have a jDialog which contains some fields that need to get focused.</p>
  
  <p>I am seeing some strange behaviour where sometimes the focusing fails, and if you hit the tab key you can see the focus change in the underlying parent window below, so clearly the focus did not get transferred.</p>
  
  <p>I read the interesting article (by camickr) on focusing: 
  <a href="http://tips4java.wordpress.com/2010/03/14/dialog-focus/" rel="nofollow">http://tips4java.wordpress.com/2010/03/14/dialog-focus/</a> 
  but that did not solve the problem.  </p>
  
  <p>Using that listener though, I was easily able to add debugging to try and see what is occurring...</p>
  
  <pre><code>public class RequestFocusListener implements AncestorListener
  {
  private boolean removeListener;
      protected static org.slf4j.Logger logger = LoggerFactory.getLogger(RequestFocusListener.class);
  
  /*
   *  Convenience constructor. The listener is only used once and then it is
   *  removed from the component.
   */
  public RequestFocusListener() {
      this(true);
  }
  
  /*
   *  Constructor that controls whether this listen can be used once or
   *  multiple times.
   *
   *  @param removeListener when true this listener is only invoked once
   *                        otherwise it can be invoked multiple times.
   */
  public RequestFocusListener(boolean removeListener) {
          logger.debug("creating RequestFocusListener, removeListener = " + removeListener);
      this.removeListener = removeListener;
  }
  
  @Override
  public void ancestorAdded(AncestorEvent e)
  {
          logger.debug("ancestorAdded detected");
          JComponent component = e.getComponent();
          logger.debug("requesting focus");
          boolean success = component.requestFocusInWindow();
          logger.debug("request focus in window result was: " + success);
          if (!success) {
              logger.debug("KeyboardFocusManager says focus failed.\nfocus owner is " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
              logger.debug("displayable="+component.isDisplayable());
              logger.debug("lightweight="+component.isLightweight());
              logger.debug("enabled="+component.isEnabled());
              logger.debug("focusable="+component.isFocusable());
              logger.debug("showing="+component.isShowing());
              logger.debug("isRequestFocusEnabled="+component.isRequestFocusEnabled());
          } else {
              logger.debug("KeyboardFocusManager says we got focus.  focus owner is " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
          }
          if (removeListener) {
      component.removeAncestorListener( this );
          }
  }
  
  @Override
  public void ancestorMoved(AncestorEvent e) {
      }
  
  @Override
  public void ancestorRemoved(AncestorEvent e) {
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Then I added the listener to a component in the main panel of the JDialog</p>
  
  <pre><code>radioButton.addAncestorListener(new RequestFocusAncestorListener());
  </code></pre>
  
  <p>The output I am getting shows:</p>
  
  <pre><code>displayable=true
  lightweight=true
  enabled=true
  focusable=true
  showing=true
  isRequestFocusEnabled=true
  </code></pre>
  
  <p>Stepping through the code to see what is making the request fail, I see it stops in Component.requestFocusHelper on: </p>
  
  <pre><code>boolean success = peer.requestFocus(this, temporary, focusedWindowChangeAllowed, time, cause);
  </code></pre>
  
  <p>I have read that the component must be displayable/visible/focusable) but the debugging shows that is ok.</p>
  
  <p>Can anyone shed any light on what else might cause the requestFocus to fail?  (and leave the focus in the calling parent panel, in this case in a jtable)</p>
  
  <p>Sorry in advance for not providing a complete SSCCE, I have tried to reproduce this in a standalone example and can't get it to fail consistently.</p>
  
  <p>I appreciate any thoughts/tips.</p>
  
  <hr>
  
  <p>followup - </p>
  
  <p>It seems like the first time I open the dialog, it gets focus, and then when I close and reopen the dialog the focus does not always get set.  </p>
  
  <p>Interestingly, after closing the dialog, if I change focus in the parent before opening the dialog again, the focus seems to always get set.</p>