JDK-6215263 : REGRESSION: paint() method not called on JFrame on JDK 1.5
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2005-01-07
  • Updated: 2010-04-02
  • Resolved: 2005-07-18
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux (Mandrake 10.1)
Windows 2000

A DESCRIPTION OF THE PROBLEM :
In JDK 1.5 overriding the paint() method (or paintComponents() for that sake), on JFrame does not work.
The paint() method is not called when the windows is opened or resized.
It IS called on restoring after being minimized and when the JFrame window
is covered by another window and the other window is removed again.



I *think* the problem appears becaurse the root pane was made opaque in JDK 1.5:

In Frame.java JDK 1.4:

    protected JRootPane createRootPane() {
        return new JRootPane();
    }

In Frame.java JDK 1.5:

    protected JRootPane createRootPane() {
        JRootPane rp = new JRootPane();
        // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
        // is NO reason for the RootPane not to be opaque. For painting to
        // work the contentPane must be opaque, therefor the RootPane can
        // also be opaque.
        rp.setOpaque(true);
        return rp;
    }


Pleas fix this as many textbooks introducing Java rely on this simple scheme for making simple graphics!

If it is best for performance that the root pane is opaque, please just state that somewhere but please revert to the JDK 1.4 code.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the includet example code on  jdk1.5.0 or jdk1.5.0_01.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A window should pop up showing some custom graphics.
  Program should immediately output 'GrafiskVindue.paint(g)'
ACTUAL -
A grey window pops up
  Program prints nothing out

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;

public class GrafiskVindue extends JFrame
{
  // Fix to make paint() work in JDK 1.5:
  //protected JRootPane createRootPane() { return new JRootPane(); }

  // Another fix that also makes paint() work in JDK 1.5:
 // { getRootPane().setOpaque(false); }

  public void paint(Graphics g)
  {
    System.out.println("GrafiskVindue.paint(g)"+new java.util.Date());
    super.paint(g); // tegn f��rst GUI-komponenterne
    g.drawLine(0, 0, 50, 50);
    g.fillOval(5, 20, 300, 30);
    g.setColor(Color.green);
    g.drawString("Hej grafiske verden!", 100, 40);
    g.drawLine(0, 0, getWidth(), getHeight());
  }


  public static void main(String[] arg)
  {
    GrafiskVindue vindue = new GrafiskVindue(); // opret vinduet
    vindue.setSize(350, 360); // s��t vinduets st��rrelse
    vindue.setTitle("GrafiskVindue"); // s��t vinduets titel
    vindue.setVisible(true); // ��bn vinduet
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Make the root pane not opaque, for example by putting the code
{ getRootPane().setOpaque(false); }
somewhere in the class.

Release Regression From : 1.4.1_06
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.
###@###.### 2005-1-07 20:45:15 GMT

Comments
EVALUATION It is very uncommon to not have an opaque root pane. The JRootPane documents this: * The painting architecture of Swing requires an opaque * <code>JComponent</code> * to exist in the containment hieararchy above all other components. This is * typically provided by way of the content pane. If you replace the content * pane, it is recommended that you make the content pane opaque * by way of <code>setOpaque(true)</code>. Additionally, if the content pane * overrides <code>paintComponent</code>, it * will need to completely fill in the background in an opaque color in * <code>paintComponent</code>. That the root pane itself was not opaque caused excessive painting. Refer to 4939857 for details. As part of 4939857 the RepaintManager would occasionally cancel a paint event that would result in painting the frame/window and instead directly invoke paint on the JRootPane. This what was causing the behavior you were seeing. As part of the gray rect fix (4967886) Swing now manages painting of top levels and this code works again:) I'm closing this bug out as a duplicate of 4967886. ###@###.### 2005-07-18 23:48:06 GMT
18-07-2005