I have a Java JApplet embedded in a web page. The JApplet window contains a single instance of a class that extends JPanel - same size as the JApplet. The applet can spawn pop-up windows (JFrames) when the user clicks a button that's in the panel.
Every time I click on a button to pop-up a window, my applet flickers as it repaints. It also does the same when I click on it again or when it had focus and I click on a different window - my conclusion: it's a repaint that's being forced on focusLost() and focusGained() events.
I am implementing double buffering in the panel's paint() method like this:
@Override
public void paint(Graphics g)
{
if(resized)
{
offscreen = createImage(getWidth(),getHeight());
resized = false;
}
Graphics offscreenG = offscreen.getGraphics();
/// DRAW HERE:
// paint the main window contents:
view.paint(offscreenG);
// paint the child components of our panel.
super.paint(offscreenG);
/// FRAW FINISHED
g.drawImage(offscreen,0,0,this);
offscreenG.dispose();
}
The view object is not a swing component but just a class that knows hot to draw everything onto a Graphics object.
The JApplet's paint() method is not overridden.
I could probably override the focusGained/focusLost methods of my JApplet to prevent repainting - but I would rather hear a better solution to the problem.