United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6215148 Swing's painting code does not paint light children unless parented to a heavyweight ancestor
JDK-6215148 : Swing's painting code does not paint light children unless parented to a heavyweight ancestor

Details
Type:
Bug
Submit Date:
2005-01-07
Status:
Resolved
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-09-20
Component:
client-libs
OS:
linux_redhat_9.0
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

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

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
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



Hardware and Software, Engineered to Work Together