title: Using JWindow for a floating tool window causes the owner window to "flicker" when clicking into the floating window
body: <h2>Background Information</h2>
  
  <p>I would like to build a tool/palette window (also called a "floating" window) in Swing with <strong>Java Version 1.6.0_26</strong>. I thought a JWindow is the best pick, and also the Swing documentation points out to use a JWindow for such purposes (floating window, which has an owner frame, has no decoration and no windows taskbar entry).</p>
  
  <p>My floating tool window's content consists of several other components like JButtons and also JTextFields.</p>
  
  <h2>The problem</h2>
  
  <p>When I click into the floating tool window, the owner window (the JFrame, my "main application window") "flickers" occasionally. The "flicker" looks like the owner window is losing the focus for a few milliseconds and than gets the focus back, resulting in a very fast disabling/enabling of the window (note that no window event is fired, like focus-lost or window-deactivated).</p>
  
  <p>I have tested this under Windows 7 64 Bit and Windows XP.</p>
  
  <h2>Video and example code</h2>
  
  <p>To clarify the problem (it is a bit difficult to explain), I've taken a video, there you can see the "flicker" of the owner window while I click onto the floating tool window repeatedly:</p>
  
  <ul>
  <li><a href="http://goo.gl/wVEqJ" rel="nofollow">http://goo.gl/wVEqJ</a> (.MOV video format, I think you need Quicktime to play it)</li>
  </ul>
  
  <p>I also put together a simple example code to reproduce the problem (this code is used in the video):</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  public class JWindowFlickerExample
  {
      public JWindowFlickerExample()
      {
          // I know swing code should be executed on the EDT,
          // just wanted to keep it simple
  
          // Create and show the "main application window"
          JFrame frame = new JFrame( getClass().getSimpleName() );
          frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
          frame.setSize( 640, 480 );
          frame.setLocationRelativeTo( null );
          frame.setVisible( true );
  
          // Create and show the "floating tool window"
          MyFloatingToolWindow testWindow = new MyFloatingToolWindow( frame );
          testWindow.setLocation( 400, 400 );
          testWindow.setVisible( true );
      }
  
      public static void main( String[] args )
      {
          new JWindowFlickerExample();
      }
  
      @SuppressWarnings( "serial" )
      private class MyFloatingToolWindow extends JWindow
      {
          public MyFloatingToolWindow( Window hostWindow )
          {
              super( hostWindow );
  
              // setFocusableWindowState( false );
              setSize( 300, 400 );
              setLayout( null );
              getContentPane().setBackground( Color.LIGHT_GRAY );
  
              JTextField textField = new JTextField();
              textField.setLocation( 50, 50 );
              textField.setSize( 70, 30 );
  
              add( textField );
          }
      }
  }
  </code></pre>
  
  <h2>Progress so far</h2>
  
  <p>I also tried to set "Window.setFocusableWindowState" to false for the floating tool window. If it is false, there is no "flicker", the problem is gone. The JavaDoc for that method points out:</p>
  
  <blockquote>
    <p>Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window."</p>
  </blockquote>
  
  <p>But then I can't use a JTextField in the floating tool window of course, because I can't focus it (maybe a text field in a floating tool window is unusual, but in my case a must).</p>
  
  <p>I guess the "flicker" effect has something to do with the focus management in some way... for the fraction of a second the floating tool window gets the focus, taking it away from the owner window and then back. But I'm not sure; as a side note: if the text field in the floating tool window has the focus, the owner window stays enabled (which is a correct behavior).</p>
  
  <p>I hope there is an easy solution, one so that I can stay with the JWindow as my floating tool window and with text fields as it's content - because, apart from the described "flicker" problem, everything works great.</p>
  
  <p>I really appreciate any help, thanks a lot!</p>