JDK-4760481 : DefaultTreeCellRenderer icons and colors aren't updated in updateUI()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-10-09
  • Updated: 2017-05-16
  • Resolved: 2011-03-08
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 7 Other
7 b03Fixed OpenJDK6Fixed
Related Reports
Relates :  
Description
Name: sv35042			Date: 10/09/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
DefaultTreeCellRenderer icons and colors aren't updated in
updateUI() .  So when one changes the look and feel and
calls SwingUtilities.updateComponentTreeUI(), the tree
icons and colors don't change.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run attached program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
I expected the icon to change to the windows icon but it
didn't.

This bug can be reproduced always.

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


class TreeBug3 {
	public static void main(String[] args) throws Exception {
		final JFrame frame = new JFrame();
		JTree tree = new JTree(new String[]{"cell"});
		tree.setCellRenderer(new DefaultTreeCellRenderer()
/*
//			Here is the fix.
			{
				// should be called by the
DefaultTreeCellRenderer constructor
				protected void init() {
					setLeafIcon(UIManager.getIcon
("Tree.leafIcon"));
					setClosedIcon(UIManager.getIcon
("Tree.closedIcon"));
					setOpenIcon(UIManager.getIcon
("Tree.openIcon"));
				
					setTextSelectionColor(UIManager.getColor
("Tree.selectionForeground"));
					setTextNonSelectionColor
(UIManager.getColor("Tree.textForeground"));
					setBackgroundSelectionColor
(UIManager.getColor("Tree.selectionBackground"));
					setBackgroundNonSelectionColor
(UIManager.getColor("Tree.textBackground"));
					setBorderSelectionColor
(UIManager.getColor("Tree.selectionBorderColor"));

//					drawsFocusBorderAroundIcon is private
but should be protected
//					Object value = UIManager.get
("Tree.drawsFocusBorderAroundIcon");
//					drawsFocusBorderAroundIcon = (value !=
null && ((Boolean)value).
//
booleanValue());
				}

				public void updateUI() {
					super.updateUI();
					init();
				}
			}
*/
		);
		frame.getContentPane().add(tree,BorderLayout.CENTER);
		frame.pack();
		frame.show();
		UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		SwingUtilities.invokeLater(new Runnable(){public void run(){
			SwingUtilities.updateComponentTreeUI(frame);
		}});
	}
}

---------- END SOURCE ----------
(Review ID: 146024) 
======================================================================

Comments
EVALUATION Since original evaluation the renderer now gets updateUI invoked on it. As such, DefaultTreeCellRenderer should override updateUI to correctly update properties based on the look and feel.
15-08-2006

WORK AROUND Reset the renderer from updateUI, eg: class MyTree extends JTree { public void updateUI() { super.updateUI(); setCellRenderer(new MyCellRenderer()); } }
24-09-2004

EVALUATION The renderers don't get updateUI as they are normally not in the containment hierarchy. This usually isn't an issue as typically the UI will reset the renderer, but not for the case when the developer has supplied one. The renderer bearing components should override updateUI and forward to the renderer/editor if the renderer existed before and after the updateUI call. And the renderers that need to should override updateUI to updated the necessary properties. ###@###.### 2003-08-11
11-08-2003