JDK-6637890 : Can't refresh container's components
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-12-05
  • Updated: 2011-04-28
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_12"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04)
Java HotSpot(TM) Client VM (build 1.5.0_12-b04, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows XP/SP2

A DESCRIPTION OF THE PROBLEM :
This problem occurs on Windows, but it works fine on Linux.

When I setBackground() in a Panel, then do Panel.repaint(), the background color isn't forwarded to its components.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the attached java program, and click Submit button.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When I click the Submit, I expect both panel and label to change the color to green.
ACTUAL -
Only panel changes the color. The label's color remain unchanged.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Here is a very simple snippet. It works properly on Linux:

import java.awt.*;
import java.awt.event.*;

public class WindowTest {
	static Frame f;
	static Image im;
	static Button b = new Button("Submit");
	static Panel p;
	static Label label;

	public static void main(String args[]) throws Exception {
		f = new Frame();
		f.setLayout(new FlowLayout());
		f.setBounds(10, 10, 500, 200);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			}
		});

		p = new Panel();
		p.setBackground(Color.red);

		label = new Label("test");
		p.add(label);
		f.add(p);
		f.add(b);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				p.setBackground(Color.green);
				p.repaint();
			}
		});
		f.setVisible(true);
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
Change the callback like below (remove then add notify), but this is not recommended way according to javadoc:

b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				p.setBackground(Color.green);
				p.repaint();
                                p.removeNotify();
                                p.addNotify();
			}
		});

Comments
EVALUATION Reproducible with at least JDK6u1 on Windows platform.
17-12-2007