title: Does JFrame.setExtendedState(MAXIMIZED_BOTH) work with undecorated frames?
body: <p>The following Swing code doesn't work correctly on my machine or my co-workers machines (all Windows XP &amp; Java 6):</p>
  
  <pre><code>public class Test {
      public static void main(String[] args) {
          final JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(800, 600);
          frame.setLayout(new FlowLayout());
          frame.add(new JButton(new AbstractAction("Maximize") {
              @Override
              public void actionPerformed(ActionEvent e) {
                  frame.setExtendedState((frame.getExtendedState() &amp; Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH ? JFrame.NORMAL : JFrame.MAXIMIZED_BOTH);
              }
          }));
          frame.setUndecorated(true);
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>It maximizes the window, but does not take into consideration the windows task bar (it fills the screen).  If you comment out "frame.setUndecorated(true);" it seems to work correctly.</p>
  
  <p>The Javadoc seems to imply this should work.  Is this a bug in Java?  Is it limited to a specific release, or version of Windows?  Is there a workaround?</p>
  
  <p>I've seen a <a href="https://stackoverflow.com/questions/6790600/java-gui-hides-windows-taskbar/6790670#comment-8891452">few workarounds</a>, but they seem incomplete.  I can't be the first person to code my own frame decorations in Java.</p>
  
  <p><strong>EDIT:</strong> After downloading the OpenJDK and digging through the native code, I found that Java is calling the win32 function <a href="http://msdn.microsoft.com/en-us/library/ms633544(v=vs.85).aspx" rel="nofollow noreferrer">SetWindowPlacement</a> and under some conditions altering <a href="http://msdn.microsoft.com/en-us/library/ms632605(v=vs.85).aspx" rel="nofollow noreferrer">MINMAXINFO</a> to get the right window size.  I think what I was seeing was the default windows behavior for a window with no title or border (although I can't find that documented anywhere).  I've found that calling JFrame.setMaximizedBounds() provides the bounds used to alter the win32 MINMAXINFO structure.  So by altering my action performed and using the window size suggested by camickr as my MaximizedBounds, I get a bit closer:</p>
  
  <pre><code>GraphicsConfiguration graphicsConfiguration = frame.getGraphicsConfiguration();
  frame.setMaximizedBounds(graphicsConfiguration.getBounds());
  frame.setExtendedState((frame.getExtendedState() &amp; Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH ? JFrame.NORMAL : JFrame.MAXIMIZED_BOTH);
  </code></pre>
  
  <p>Now the maximize window no longer hides the menubar.  But.. now I have another problem.  I can't maximize from a secondary monitor. The frame just disappears.</p>
  
  <p>I'm adding some win32 tags in hopes of getting a C++ programmer who recognizes this problem. </p>
  
  <p><strong>SOLUTION:</strong> I apparently can't answer my own question yet, so I'll just put my solution here.  I had to use a Sun class, and I haven't tested it on any platforms other than Windows, but so far this seems to be working well with multiple monitors, and multiple taskbar configurations:</p>
  
  <pre><code>GraphicsConfiguration config = frame.getGraphicsConfiguration();
  Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
  frame.setMaximizedBounds(new Rectangle(0, 0, usableBounds.width, usableBounds.height));
  frame.setExtendedState((frame.getExtendedState() &amp; Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH ? JFrame.NORMAL : JFrame.MAXIMIZED_BOTH);
  </code></pre>