Treeview, change node icon when treeview is disabled

For the treeview component, we can modify, node icon’s.
But when the treeview is disabled, a default node icon (file icon) is used instead of the node icon’s (Icon and SelectedIcon column in the tree view dataset) ?
:scratch:

Did you ever resolve this? I’m facing the same issue.

No workaround found for this case :sob:

The disabled icon comes from the underlying DefaultTreeCellRenderer’s getTreeCellRendererComponent (see here, line 472) which calls out to the look-and-feel to return a grey-tinted version of the ‘default’ leaf icon (the page/folder, respectively). To override that behavior, you would have to provide a different implementation of TreeCellRendererComponent to the JTree inside the PMITreeView component.

Thanks, got it to work. I’ve included my script below.
The script is a custom method on the Tree View component.

def setupTreeCellRenderer(self):
	"""
	Arguments:
		self: A reference to the component instance this method is invoked on. This argument
		  is automatic and should not be specified when invoking this method.
	"""
	from javax.swing.tree import DefaultTreeCellRenderer
	from javax.swing import UIManager
	
	# Create a subclass of DefaultTreeCellRenderer
	class CustomRenderer(DefaultTreeCellRenderer):
	
		originalRenderer = None
		
		# init
		def __init__(me, tree):
			me.originalRenderer = tree.getCellRenderer()
		
		# Use the original cell renderer to get a Tree Cell Renderer Component, then simply
		# modify the icon it uses.
		def getTreeCellRendererComponent(me, tree, value, sel, expanded, leaf, row, hasFocus):
			tcrc = me.originalRenderer.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus)
			if not tree.isEnabled():
				icon = tcrc.getIcon()
				laf = UIManager.getLookAndFeel()
				# Get a disabled (grey) version of the icon
				disabledIcon = laf.getDisabledIcon(tree, icon)
				if (disabledIcon != None): icon = disabledIcon
				# Set the disabled icon
				tcrc.setDisabledIcon(icon)
			return tcrc
	
	# Assign the cell renderer of the component's underlying tree view
	# to a new instance of our custom Renderer class
	tree = self.getComponent(0).getComponent(0)
	tree.setCellRenderer(CustomRenderer(tree))
1 Like