JDK-6215148 : Swing's painting code does not paint light children unless parented to a heavyweight ancestor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux_redhat_9.0
  • CPU: x86
  • Submitted: 2005-01-07
  • Updated: 2010-04-02
  • Resolved: 2005-09-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b53Fixed
Description
Swing's painting code does the following check to avoid painting heavyweight children:

if (isLightweightComponent(comp)) {
  ... paint child ...
}

isLightweightComponent will return false if the component is a swing component but not parented to a heavy weight component.  This means if you paint a containment hierarchy to an image only the component you invoke paint on will be painted.

Here's a test case:

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;

public class PaintTest {
    
    public static void main(final String[] args) throws Exception {
        JPanel panel = new JPanel(new FlowLayout());
        JButton b = new JButton("Button");
        JLabel l = new JLabel("Label");
        l.setOpaque(true);
        JTree t = new JTree();
        panel.add(b);
        panel.add(l);
        panel.add(t);
        panel.setSize(panel.getPreferredSize());
        panel.doLayout();
        
        saveComponentImage(panel, "panel.jpg");

        Window window = new JWindow();
        window.add(panel);
        window.pack();
        saveComponentImage(panel, "panel2.jpg");
        window.dispose();
    }
    
    private static void saveComponentImage(JComponent comp, String filename) throws Exception {
        BufferedImage im = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = im.getGraphics();
        comp.paint(g);
        g.dispose();
        ImageIO.write(im, "JPEG", new File(filename));
    }
}

###@###.### 2005-1-07 17:05:03 GMT

Comments
EVALUATION In investigating this there is a second problem. BufferedStrategyPaintManager was not correctly handling a non-Window/Applet parent. It would throw an NPE. I'm fixing this at the same time too.
2005-09-06

EVALUATION Enough people have bumped into this, or asked me about it that we should really fix this. Possible solutions: . Override isLightweight in JComponent to always return true. I'm worried this will have backward compatability issues though. . Have paintChildren check for instanceof JComponent first. I'm leaning toward the second option. ###@###.### 2005-07-12 19:46:17 GMT
2005-01-07