Re-display problems with the Sun JVM

In some environments the Sun Java Virtual Machine has trouble re-displaying the components in this applet.  Although each panel is displayed properly the first time it is shown, on each subsequent display of the same panel there is a long delay before its components appear.

To reproduce this problem, click the button "Click to leave panel 0".  A new panel is displayed immediately with a button "Click to leave panel 1".  Clicking that button results in a long delay before the button "Click to leave panel 0" is displayed on the original panel.  Any subsequent attempt to display the contents of either panel takes a long time, though the panel itself seems to be loaded, as shown by the background color.

This problem occurred with some Java 1.3.x versions on Windows 98, but does not occur with 1.4.2_02 on Windows 98.  However, it occurs with 1.4.2_02 on Windows XP.  This problem does not occur on the Macintosh or with the Microsoft Java Virtual Machine on Windows, though there is a different display problem on the Macintosh shown here.

A workaround is to create the panels new instead of re-adding existing panels, but this can be non-trivial to implement in existing code.

This bug is fixed in Sun Java 1.5.0 Update 1.

If you have any comments please contact Mickey Segal.  A listing of  many Java resources is at this link.

Source code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ReDisplay extends Applet implements ActionListener {

ButtonPanel[] panel;
Color[] panelColor;
static final int number = 2;

public void init()
{
    panel = new ButtonPanel[number];
    panelColor = new Color[number];
    panelColor[0] = Color.red;
    panelColor[1] = Color.blue;
    for (int i=0; i<number; i++)
    {
        panel[i] = new ButtonPanel(i, panelColor[i], this);
    }
    add(panel[0]);
}

public void actionPerformed(ActionEvent ae)
{
    for (int i=0; i<number; i++)
    {
        if (ae.getSource() == panel[i].button)
        {
            remove(panel[i]);
            if (i<(number - 1))
            {
                add(panel[i+1]);
                panel[i+1].invalidate();
            }
            else
            {
                add(panel[0]);
                panel[0].invalidate();
            }
            invalidate();
            validate();
        }
    }
}
} // END OF Class ReDisplay


class ButtonPanel extends Panel {

Button button;
int choicesUsed = 10;

ButtonPanel(int panelNumber, Color color, Container parentContainer)
{
    setBackground(color);
    button = new Button("Click to leave panel " + String.valueOf(panelNumber));
    add(button);
    button.addActionListener((ActionListener)parentContainer);
}
} // END OF Class ButtonPanel