So I have a dropdown dataset and I would like the user to know if the vessel he is trying to select is offline by changing the color of the corresponding row. Dropdown mode is set to "List". So when you press on the dropdown list you should see Vessel 1 and Vessel 6 with red foreground. The dataset is being updated every 5 seconds by a script with the online/offline status changes.
I tried the styles dataset on appearence but it modifies the foreground color of the whole table. Furthermore, I can't make it "read" from the "Online" column in the dataset, only from "Value" and "Label".
I thought about a propertyChange script on source.data, but I guess I would have to use something like from javax.swing import JComboBox and that's above my knowledge level.
It can be done by implementing a custom ComboListCellRenderer
.
Example:
# Run once at initialization
# Preview mode has to be active prior to opening the window for this event to occur in the designer
if event.propertyName == 'componentRunning' and event.newValue:
from com.inductiveautomation.plaf import ComboListCellRenderer
# Create a custom cell renderer implementation for coloring the dropdown rows
class ColorCellRenderer(ComboListCellRenderer):
def getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus):
ComboListCellRenderer.getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus)
# active row highlight
if cellHasFocus or isSelected:
self.background = event.source.selectionBackground
self.foreground = event.source.foreground
# conditional coloring
elif index != -1:
online = event.source.data.getValueAt(index, 'Online')
self.background = system.gui.color('green') if online else system.gui.color('red')
self.foreground = system.gui.color('white') if online else system.gui.color('yellow')
return self
# Repalce the dropdown's stock renderer with an instance of the custom implementation
event.source.setRenderer(ColorCellRenderer())
Result:
Here are some other forum examples for reference:
• Listbox Foreground-or Background Dynamic Color Change via Python
• A color picker component for Vision
• Change the background and icon color of the Dropdown Component's button
1 Like