Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate JFrame maximized bounds while keeping it maximized
    text
    copied!<p>Is it possible to update the maxized bounds of a JFrame (visibly) after calling setMaximizedBounds() while the JFrame is maximized?</p> <pre><code>// I do not have access to / cannot change the following lines JFrame frame = new JFrame(); frame.setVisible(true); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // frame fills the whole screen at this point // My code: frame.setMaximizedBounds(newBounds); </code></pre> <p>I currently do the following to update the JFrame location and size:</p> <pre><code>frame.setExtendedState(JFrame.NORMAL); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); </code></pre> <p>but this causes the window to visually get restored and then maximized again, but with the correct maximized bounds.</p> <p>I tried <code>frame.doLayout()</code> and <code>frame.getContentPane().doLayout()</code> but the frame does not update to the new bounds.</p> <p><strong>EDIT</strong> SSCCE:</p> <p>Adapted from <a href="https://stackoverflow.com/q/7403584/1196618">here</a>.</p> <p>As mentioned in the comments the JFrame is actually created and initialized by the NetBeans RCP so I cannot override it (as is proposed <a href="https://stackoverflow.com/a/6422995/1196618">here</a>) as a workaround for <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788" rel="nofollow noreferrer">Java bug 4737788</a> (which I am trying to create a workaround for). The workaround works but I would like to avoid the (hack) restore &amp; maximize to update the frame.</p> <pre><code>import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GraphicsConfiguration; import java.awt.Rectangle; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import sun.java2d.SunGraphicsEnvironment; public class MaximizedBoundsTest { 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("Fix Maximized Bounds") { @Override public void actionPerformed(ActionEvent e) { // Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788 // to take Windows taskbar into account GraphicsConfiguration config = frame.getGraphicsConfiguration(); Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice()); frame.setMaximizedBounds(usableBounds); // Hack to update frame size &amp; location (is there a better way to do this?) frame.setExtendedState(Frame.NORMAL); frame.setExtendedState(Frame.MAXIMIZED_BOTH); } })); frame.setExtendedState(Frame.MAXIMIZED_BOTH); frame.setUndecorated(true); frame.setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload