Can a listbox item be highlighted based on a condition?

I have a dropdown list that I use the selected value to populate a 24 hour data table. There are 40 widgets to choose from in the list box. The 24 hour data table values are stored in a PLC. I have a request to highlight the available dropdown items if it contains non-zero data in the 24 hour table. I can create PLC code to generate a boolean tag for each of the 40 widgets if all values are zero, that's no issue and probably easier than trying to script that in Ignition (maybe?). But is there a way to bind to the individual items in the dropdown box so that all the non-zero items in the list are highlighted?

Edit: Vision environment

Perspective or Vision will be important information. Add the appropriate tag to your question title (using the pencil edit icon).

Thanks...sorry 'bout that.

Maybe an easier way to explain.

If I have a drop down box with 5 string labels, called

1234
1345
1456
4567
5678

Is there a way to highlight all the ones that start with 1 (the first three)? Is there a way to change the font even, make it bold for example, of individual items in the list?

Yes; this should be doable with the list cell renderer. Here is an example I developed for simply changing the dropdown background color independent of the component color

Looking at the code in that example. The part to modify for your use case would be this:

if cellHasFocus or isSelected:
	component.background = event.source.selectionBackground
else:
	component.background = Color.black

Just add some elif statements to for highlighting values greater than zero or for changing fonts. Note the variables that are passed into renderer component that can be used in your scripting:

(self, list, value, index, isSelected, cellHasFocus)
2 Likes

I found a few moments this morning to throw a dropdown in my test environment and develop the code.
Here is result:
image

Here is the script: # Written for the propertyChange event handler of the dropdown component

from javax.swing import DefaultListCellRenderer
from java.awt import Color, Font

# assign am easy to read name to the source component
dropdown = event.source

# Define a custom cell renderer class that extends the DefaultListCellRenderer
class ColorCellRenderer(DefaultListCellRenderer):

	# Overriding the `getListCellRendererComponent` method to customize the rendering of cells in the dropdown
	def getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus):

		# Call the parent class's method to get the default rendered component
		listCellComponent = DefaultListCellRenderer.getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus)
		
		# If the current cell has focus or is selected, set its background to the dropdown's selection background color
		if cellHasFocus or isSelected:
			listCellComponent.background = dropdown.selectionBackground
		
		# If the data associated with the cell (in column 1) starts with '1', set its background to yellow and make its font bold
		# This assumes that the dataset that is populating the dropdown uses column 0 for value and column 1 for label
		elif index > -1 and str(dropdown.data.getValueAt(index, 1)).startswith('1'):
			listCellComponent.background = Color.yellow
			listCellComponent.font = listCellComponent.font.deriveFont(Font.BOLD)
		
		# For all other cells, set the background to white and do nothing with the font
		else:	
			listCellComponent.background = Color.white
		
		# Set the foreground (text) color of the cell to black for all cells
		listCellComponent.foreground = Color.black
		return listCellComponent

# apply renderer when the window is first opened and the component is started
if event.propertyName == 'componentRunning':
	# Create an instance of the custom ColorCellRenderer()
	renderer = ColorCellRenderer()
	
	# set the dropdown's renderer to the custom renderer
	dropdown.setRenderer(renderer)

Note: To observe the effects of this code in the designer, preview mode must be playing when the window is opened. Otherwise the componentRunning property change event won't occur.

3 Likes

Awesome....ill give it a shot next week. Thanks!!