Splash Screen Woes or Why doesn't setVisible(true) protect from setVisible(false)




For the last week I've been tracking down a problem that has absolutely
made me
crazy. My program is a near real time GUI that monitors a piece of
audio equipment.
We upgraded to Java 1.5 recently and started getting a white screen up
startup.
Not every time mind you. My test machines seem to want to do it about 1
time in 100
while our field machines were doing it a lot more often. The background
processes
of the application would run fine but the GUI was just a blank screen
(white).

I finally tracked the problem down to the splash screen that would be
displayed
while waiting for certain synchronizations to occur. It seems that, on
some
occasions, the code that creates the splash JFrame and calls
setVisible(true) was
being followed by estVisible(false) a little too quickly for Java's
liking and the
empty JFrame (no message was displayed) would come up and stay up.

I'm posting primarily to see if maybe I'm missing something and there's
a problem with my code (see below) or, alternatively, if others have
encountered this problem
and what they did about it. I really dislike putting sleeps into a
program because
events are not properly synchronized in Swing but at this point I don't
know what
else to do (except drop the screen altogether).


These are the two methods. When called with a delay they work fine but
when called
one right after the other, I get a white screen about 3 times in 5.

////////////////////////////////////////////////////////////////

public static void showMessageWindow(String msg)
{
synchronized (classLock)
{
if (messageWindow == null)
{
Dimension d =
Toolkit.getDefaultToolkit().getScreenSize();
int w = d.width;
int h = d.height;

log.info ( "Creating window message panel of width " +
w +
" and height " + h);

messageWindow = new JFrame();
messageWindow.setUndecorated(true);

messageWindowLabel = new JLabel();
messageWindowLabel.setHorizontalAlignment (
SwingConstants.CENTER);
messageWindowLabel.setFont(new Font("Dialog",
Font.BOLD, 16));
messageWindowLabel.setForeground(Color.black);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(messageWindowLabel, BorderLayout.CENTER);
panel.setBounds(0, 0, w, h);

messageWindow.getContentPane().setLayout(null);
messageWindow.getContentPane().add(panel);
messageWindow.setBounds(0, 0, w, h);
}

log.info("Showing message window: " + msg);

messageWindowLabel.setText(msg);

messageWindow.setVisible(true);
}
}


public static void hideMessageWindow()
{
synchronized (classLock)
{
log.info("Hiding message window");

messageWindow.setVisible(false);
}
}

////////////////////////////////////////////////////////////////////

.