PowerTable Item Listener

I'm running Ignition 8.1.26 and am having some trouble with an Item Listener on the configureEditor extension function on the Power Table. I'm looking to get a drop down component in the cells of the specified column which I'm able to do. I'm using the PMIComboBox instead of a JComboBox because I'd like to use a dataset in the PMIComboBox and get other column values within that dataset based on the user selection. The code that I have is working but the itemStateChanged in the listener part is firing 2 times on a single selection. The code is below and any direction or assistance is greatly appreciated. Thank you.

def configureEditor(self, colIndex, colName):
	from javax.swing import AbstractCellEditor,JComboBox, JTextField
	from javax.swing.table import TableCellEditor
	from java.awt.event import ItemListener, ItemEvent
	from com.inductiveautomation.factorypmi.application.components import PMIComboBox
	myData = self.adhTest # custom property dataset
	### Construct a custom Item Listener ##
	class custItemListener(ItemListener):
		def __init__(self, table, value, rowIndex, colIndex):
			self.table = table
			self.rowIndex = rowIndex
			self.colIndex = colIndex
			self.value = value
		def itemStateChanged(self, event):
			if event.getStateChange() == ItemEvent.SELECTED:
				### Process any necessary changes on state change event ###
				# get the selected item
				newValue = event.getItem()
				print "itemStateChange newValue",newValue
				# update the dataset with the new value
				newData = system.dataset.setValue(self.table.data, self.rowIndex, self.colIndex, newValue)
				self.table.data = newData
				### For testing set the same value to column index 2 # occurs when event is fired.
				newData = system.dataset.setValue(self.table.data, self.rowIndex, 2, newValue)
				self.table.data = newData
				# print the updated value
				print 'item state change',self.table.data.getValueAt(self.rowIndex, 2)
				# stop the cell edit on the table if it's being edited.
				tableCellEditStatus = self.table.getTable().isEditing()
				# if cell edit is in process stop the cell editing.
				if tableCellEditStatus == True:
	# create and use the myTableCellEditor class to access the JTable/Jide Table cell editor 
	# and configure the cell editor based on specific conditions
	class myTableCellEditor(TableCellEditor, AbstractCellEditor):

		# construct the class
		def __init__(self, tableComponent):
			self.table= tableComponent
			self.editor = None
		def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, colIndex):
			rowVal = self.table.data.getValueAt(rowIndex,0)
			displayDD = 0 # this is a flag that will indicate to display the dropdown
			# if column index is 0,  set the drop down options
			if colIndex == 0:
				# set the ddOptions to data
				self.ddOptions = myData	
				# set a drop down display flag: 0 - hide, 1 - display
				displayDD = 1

			# if it's ok to display the drop down, set the editor
			# set the editor as PMIComboBox with the ddOptions as a parameter	
			if displayDD == 1:
				self.editor = PMIComboBox()
				self.editor.setData(myData)	#set the data for the drop down
				self.editor.setEditable(False) # if edit is allowed True else False
				ddSelIndex = self.editor.getSelectedIndex()
				# add the item listener
				self.editor.addItemListener(custItemListener(self.table, value, rowIndex, colIndex))

			# else set None as the editor
				self.editor = None
			return self.editor
		def getCellEditorValue(self):
			if self.editor.selectedValue > -1:
				if self.editor.getSelectedIndex() > -1:
					# this method gets the editor value(s) for the comboBox and print them
					newValue = self.editor.getSelectedItem() # selected Item
					newLabel = self.editor.getSelectedLabel() # selected Label
					newObj = self.editor.getSelectedObjects() # selected Object (Array)
					newSelValue = self.editor.getSelectedValue() # selectedValue
					newSelStrValue = self.editor.getSelectedStringValue() # selected String Value
					newIndex = self.editor.getSelectedIndex() # selected Index
					adhValue = self.editor.data.getValueAt(newIndex,2) # value at specified column at specified index in the ds
					newValue = '<Select One>' # Can use None as new value
					newIndex = -1
				print '--->New Value<---', newValue, newIndex
				return newValue

	# if the selected column is Shift get the new editor and return it		
	if colName == "Shift":
		myEditor = myTableCellEditor(self)	
		return {"editor":myEditor}

Many of the PMI* components do odd things with selections such that multiple propertyChanges happen. You will probably have to move that dataset to a custom property on the power table and have a regular JComboBox editor refer to it.

Maybe use a regular JComboBox, but use a DataSetComboBoxModel?


1 Like

Thank you for the information. I’ll keep that in mind going forward.

Thank you for the information I’ll use the JComboBox with this model.

Thank you PGriffith and pturmel. Using the dataset model worked and item listener is behaving as it should. Your help is very much appreciated.

1 Like