OK I’ve added an ItemListener to the Combobox to get the item changed event as soon as it’s changed:
In configureEditor use this:
from javax.swing import AbstractCellEditor,JComboBox, JTextField
from javax.swing.table import TableCellEditor
from java.awt.event import ItemListener, ItemEvent
# used to listen to item change events in the combobox
class MyItemListener(ItemListener):
# Constructor
def __init__(self, table, value, rowIndex, colIndex):
self.table = table
self.rowIndex = rowIndex
self.colIndex = colIndex
self.value = value
self.options = {"OFF":0, "Hand":1, "Auto":2}
# this is fired when the combobox changes
def itemStateChanged(self, event):
if (event.getStateChange() == ItemEvent.SELECTED):
item = event.getItem()
# convert the new value to the index. E.g. OFF -> 0
if item in self.options:
newValue = self.options[item]
# create a new dataset with the updated value
newDS = system.dataset.setValue(self.table.data, self.rowIndex, self.colIndex, newValue)
# write back to the source property
# update this to whereever your master data is
self.table.parent.DS = newDS
class myTableCellEditor(TableCellEditor, AbstractCellEditor):
def __init__(self, tableComponent):
self.table= tableComponent
self.editor = None
# defind the options for the drop down
self.options = {"OFF":0, "Hand":1, "Auto":2}
def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, colIndex):
# work out if the row should show a combobox or text box.
if rowIndex in [0,1,2,3]:
# create a combo box with the options
self.editor = JComboBox(self.options.keys())
self.editor.setSelectedIndex(value)
# add an Item changed listener
self.editor.addItemListener(MyItemListener(self.table, value, rowIndex, colIndex))
else:
# create a text field for default editor
self.editor = JTextField()
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[selectedValue]
else:
# get the text field value
newValue = self.editor.getText()
return newValue
if colName == "Vrednost":
m = myTableCellEditor(self)
return {"editor":m}
Keep this the same in configureCell:
from javax.swing import JComboBox
data = {}
if colName == 'Vrednost':
if rowIndex in [0,1,2,3]:
options = {0:"OFF", 1:"Hand", 2:"Auto"}
if value in options:
cb = JComboBox(["OFF", "Hand", "Auto"])
cb.setSelectedIndex(value)
data['renderer'] = cb
return data
Now that the item is being updated in the configureEditor function, you need to prevent it from trying to update twice, so use this in the onCellEdited:
if colName == 'Vrednost':
if rowIndex not in [0,1,2,3]:
self.parent.DS = system.dataset.setValue(self.data, rowIndex, colIndex, newValue)
I’ve tested it and it works pretty well for me.
Good luck.