I’m playing with “javax.swing.table.TableCellEditor” to make the table a little bit special.
I have a strange phenomenon when the component is a JCheckBox:
a small blue rectangle appears to the left of the cell.
has anyone else noticed this phenomenon?
from javax.swing import AbstractCellEditor,JComboBox, JTextField, JCheckBox, SwingConstants
from javax.swing.table import TableCellEditor
from java.awt.event import ItemListener, ItemEvent
import json
from shared.tools.pretty import p
class myTableCellEditor(TableCellEditor, AbstractCellEditor):
def __init__(self, tableComponent):
self.table = tableComponent
self.editor = None
# defind the options for the drop down
self.options = []
def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, colIndex):
#print 'sono qua'
def str2bool(value):
return value.lower() in ("yes", "true", "open", "on", "t", "1")
self.options = json.loads(self.table.data.getValueAt(rowIndex,'options'))
try:
if 'edit' in self.options:
type = self.options['edit']
print type
if type in ['checkBox','comboBox']:
if type == 'checkBox':
print 'sono un check box'
self.editor = JCheckBox()
self.editor.setSelected(str2bool(value))
self.editor.setHorizontalAlignment(SwingConstants.CENTER)
elif type == 'comboBox':
print 'sono un combo box'
if self.options['translation list']:
lst_k = [k for k,v in self.options['translation list'].iteritems()]
lst = [int(v) for k,v in self.options['translation list'].iteritems()]
print lst_k
print lst
index = 0
try:
val = int(value)
except:
val = 0
if val in lst:
index = lst.index(val)
self.editor = JComboBox(lst_k)
self.editor.setSelectedIndex(index)
else:
print 'non so cosa sono'
else:
# create a text field for default editor
self.editor = JTextField(value)
print 'sono un textfield'
except:
self.editor = JTextField()
print 'disastro'
return self.editor
def getCellEditorValue(self):
# is the editor a combobox?
if type(self.editor) is JComboBox:
# get the combobox value
selectedValue = self.editor.getSelectedItem()
# convert back to the options value
newValue = self.options['translation list'][selectedValue]
elif type(self.editor) is JCheckBox:
return self.editor.isSelected()
else:
# get the text field value
newValue = self.editor.getText()
return newValue
if colName == "value":
m = myTableCellEditor(self)
return {"editor":m}