I am having difficulties displaying a JFrame
with all of the contents showing immediately. I would like to show the JFrame
with all Components
already added, but the window appears to become visible out-of-focus, wait a second, then come into focus with the Component
(s) added. The code for my SSCCE is extremely short/simple, so I don't know why this effect is happening.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DelayTest
{
public static void main(String[] args)
{
JLabel label = new JLabel("DELAY TEST");
label.setHorizontalAlignment(JLabel.CENTER);
label.setPreferredSize(new Dimension(400, 60));
JFrame frame = new JFrame("Delay Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I know that this effect does not happen if I comment out label.setPreferredSize(new Dimension(400,60));
, so though it may simply be a correlation & not a causation, I'm guessing somehow PreferredSize
s have to do with it. But, why?
ALSO, this effect occurs even if I create the GUI in a separate Runnable
thread apart from the main thread. Although clearly the effect is related to the order of thread execution, I do not believe that specific factor is the cause.
Upon startup:
About 1 second later: