Recently, I was messing around with painting the cells of a power table a gradient color, and the process gave me an idea on an alternative approach to this old problem. Combining what I learned painting the gradient cells and code I had previously developed for changing the checkbox size on the alarm status table, I was able to produce this work around using the configureCell extension function:
The code checks each column to see if the column class is Boolean. If not, it colors the rows the normal way, but if it is a bool, it creates an image icon to put in place of the JCheckbox. To make the checkbox look good when toggling, I had to set the column to not editable, and use the onMouseClick extension function to change the value in the dataset. Otherwise, it looked kind of funny because the editor would override the painting and turn the whole cell white for a second.
Here is the configureCell script:
#def configureCell(self, [...]):
from java.lang import Boolean
from java.awt import BasicStroke
from java.awt.image import BufferedImage
from javax.swing import ImageIcon
cellRect = self.table.getCellRect(rowIndex, colIndex, True)
squareDims = 12
margin = 14
x = int(0.5 * float(cellRect.width - squareDims - margin))
y = int(0.5 *float(cellRect.height - squareDims - margin))
if self.table.getColumnClass(colView) != Boolean:
if selected:
return {'background': self.selectionBackground}
if rowView % 2 != 0:
return {'background': 'gray', 'foreground': 'white'}
else:
return {'background':'black', 'foreground': 'white'}
else:
renderer = self.table.defaultCellRenderer
if selected:
renderer.background = self.selectionBackground
elif rowView % 2 != 0:
renderer.background = system.gui.color('gray')
else:
renderer.background = system.gui.color('black')
checkedImage = BufferedImage(cellRect.width - margin, cellRect.height - margin, BufferedImage.TYPE_INT_ARGB)
checkBox = checkedImage.createGraphics()
if self.data.getValueAt(rowIndex, colIndex):
checkBox.setPaint(system.gui.color('blue'))
else:
checkBox.setPaint(system.gui.color('white'))
checkBox.fillRect(x, y, squareDims, squareDims)
checkBox.setColor(system.gui.color('white'))
checkBox.setStroke(BasicStroke(2))
checkBox.drawLine(x + 2, y + 5, x + 4, y + 8)
checkBox.drawLine(x + 4, y + 8, x + 10, y + 2)
renderer.icon = ImageIcon(checkedImage)
renderer.text = None
return {'renderer':renderer}
Here is the onMouseClick script:
#def onMouseClick(self, [...]):
from java.lang import Boolean
if self.table.model.getColumnClass(colIndex) == Boolean:
currentValue = self.data.getValueAt(rowIndex, colIndex)
self.data = system.dataset.setValue(self.data, rowIndex, colIndex, not currentValue)
Edit:
I still had the original table I used during my earlier attempt. Here's what it looks like with the updated approach: