Hello There,
I am trying to format background color of the power table based on the value of previous column.
Here’s an example:

In the picture below, there are 6 rows with same column value of batch_number hence, I want to have those rows grouped in same color and so on.
Here’s what I got but I think I am missing something:
data = self.data
returnValue = {}
if rowView == 0:
returnValue["background"] = '#DDDDDD'
previous = '#DDDDDD'
elif colName == 'batch_number':
if int(value) == int(data.getValueAt(rowIndex-1, colIndex)):
returnValue["background"] = previous
else:
if previous == 'white':
returnValue["background"] = '#DDDDDD'
previous = '#DDDDDD'
else:
returnValue["background"] = 'white'
previous = 'white'
return returnValue
Any leads are appreciated.
Thanks,
Kaushik
Here’s something I haven’t tested but should help. If your batch numbers skip around quite a bit you might end up having more than 1 consecutive group with the same color. If so then this script would need to be further refined.
rowColors = ["#EEEEEE", "#AAAAAA", "#777777"]
batch_number = self.data.getValueAt(rowIndex, "batch_number ")
return {"background": rowColors[ batch_number % len(rowColors) ] }
You can put as many colors in the rowColors list and the script will choose a color based on the modulus
1 Like
@JGJohnson Thanks so much. That got me going.
I’m looking at doing this as well. My following script works, but it sometimes gets messed up when I select a row
rowColors = ['#CCFFCC', 'white']
if colName == 'status':
from javax.swing import JComboBox
elements = [self.host,'Retrived','Not Found']
dropdown = JComboBox(elements)
dropdown.setSelectedItem(value)
return {'renderer':dropdown}
else:
if colName == 'serial':
serial = self.data.getValueAt(rowIndex,'serial')
if rowIndex:
prevSerial = self.data.getValueAt(rowIndex-1,'serial')
else:
self.toggle = 0
prevSerial = serial
if serial != prevSerial:
self.toggle = not self.toggle
if selected:
return {'background': self.selectionBackground}
else:
return {'background': rowColors[self.toggle]}
Correct Rendering:
Incorrect after selection:
I found a way to make it faster and therefore the row selection does not mess up any more 
First, I made a custom method on the table called mapColors()
, it creates a list of unique values, serial numbers in my case, and keeps them in order according to how they appear in the table. I multiply my color list by the length of the set in order to make sure I have enough alternations to cover every serial number. The color mapping dictionary is then put in a client property.
from collections import OrderedDict
serials = self.data.getColumnAsList(self.data.getColumnIndex('serial'))
set = []
for serial in serials:
if serial not in set:
set.append(serial)
colors = ['CCFFCC','white'] * len(set)
mapping = {set[i]: colors[i] for i in range(len(set))}
#print mapping
self.putClientProperty('colorMapping',mapping)
Next, I call mapColors()
anytime the data changes
if event.propertyName == 'data':
event.source.mapColors()
Finally, configureCell
call the color mapping client property and applies the correct color. I only call the colorMapping once when I’m in the ‘serial’ column and put it in another client property so I don’t have to get the serial number for every single cell in the row.
if colName == 'status':
from javax.swing import JComboBox
elements = [self.host,'Retrived','Not Found']
dropdown = JComboBox(elements)
dropdown.setSelectedItem(value)
return {'renderer':dropdown}
elif selected:
return {'background': self.selectionBackground}
else:
if colName == 'serial':
color = self.getClientProperty('colorMapping')[self.data.getValueAt(rowIndex,'serial')]
self.putClientProperty('cellColor',color)
return {'background': self.getClientProperty('cellColor')}