Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6215148
Votes 4
Synopsis Swing's painting code does not paint light children unless parented to a heavyweight ancestor
Category java:classes_swing
Reported Against
Release Fixed mustang(b53)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs
Submit Date 07-JAN-2005
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  customer  = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g =  customer .getGraphics();
        comp.paint(g);
        g.dispose();
        ImageIO.write( customer , "JPEG", new File(filename));
    }
}

  xxxxx@xxxxx   2005-1-07 17:05:03 GMT
Work Around
N/A
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.
  xxxxx@xxxxx   2005-07-12 19:46:17 GMT
Posted Date : 2005-07-12 19:46:17.0

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.
Posted Date : 2005-09-06 14:46:35.0
Comments
  
  Include a link with my name & email   

Submitted On 09-FEB-2007
multik
And how can i override isLightWeight in JComponent?



PLEASE NOTE: JDK6 is formerly known as Project Mustang