Your problem intrigued me. After some investigation, I think I confirmed something that I remember about setting the state of the window (maximized, restored, etc.), which is that setting the state is a request to the operating system and left whim of the OS to process the request. This means that it is asynchronous, or at least done later, after installing it. I confirmed the use of logging and the addition of resize listeners, where you can see that the frame size changes after the code exits the code. Because of this, package () will compose components at their preferred size. Imagine that the frame size is 800x600, and the components are arranged as such (the horizontal button is about 400). Then later, the OS will change the frame size to full screen (for example, 1024x768) - for a moment you will see that the button is still at 400. Then the frame processes the new size and shifts the components and centers on the button to about 512. Thus, you will see flicker when it goes during this process. Perhaps the solution is to NOT pack () - it will remain at zero and the user will see minimal flicker.
First try this change:
If this looks good, then you may have the following problem ... if the user presses the restore button, the entire frame is compressed into a black hole. Therefore, try calling the packet AFTER the frame was predicted to be resized due to maximization. Something like that:
f.addComponentListener( new ComponentAdapter( ComponentEvent e ) { public void componentResized( Component) { if( f.getSize().getWidth() > 0 ) { e.getComponent().removeComponentListener( this ); ((JFrame)e.getComponent()).pack(); } } }
So, if the user later clicks the restore button, the frame will have a beautifully packaged size, ready to go.
- Update
OK, last try. Although I think my description of the problem has some truth, my proposed solutions did nothing. Here is the last attempt. Remove package () and setPreferredSize () and replace it by setting the size to the screen size. This seems to significantly reduce the flicker of my system. This is because there should be no difference between the original layout and the maximized layout done later. You can see this if you switch between recovery and maximum. Although I still see very little flicker when switching the two, at least it seems to look better when it is first displayed.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TEST { public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { System.out.println("Debug test..."); JPanel btnPnl = new JPanel(); btnPnl.add(new JButton("TEST")); JFrame f = new JFrame("TEST"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add(btnPnl);
-Mike
source share