# PowerTable Item Listener

**URL:** <https://forum.inductiveautomation.com/t/powertable-item-listener/77490>\
**Category:** Ignition\
**Tags:** ignition81, vision, scripting\
**Created:** [August 5, 2023, 4:16pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490 "2023-08-05T16:16:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ademovicemir](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ademovicemir](https://forum.inductiveautomation.com/u/ademovicemir)\
**Post date:** [August 5, 2023, 4:16pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/1 "2023-08-05T16:16:47Z")

</div>

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.

```auto
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:
					self.table.getTable().getCellEditor().stopCellEditing()
				
	# 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()
				self.editor.setSelectedValue(ddSelIndex)
				# add the item listener
				self.editor.addItemListener(custItemListener(self.table, value, rowIndex, colIndex))

			# else set None as the editor
			else:
				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
				else:
					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}

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 5, 2023, 4:44pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/2 "2023-08-05T16:44:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 5, 2023, 4:57pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/3 "2023-08-05T16:57:45Z")

</div>

Maybe use a regular JComboBox, but use a DataSetComboBoxModel?

[https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.30/com/inductiveautomation/factorypmi/application/components/PMIComboBox.DataSetComboBoxModel.html](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.30/com/inductiveautomation/factorypmi/application/components/PMIComboBox.DataSetComboBoxModel.html)

---

<div class="post-metadata">

**Author:** ![ademovicemir](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ademovicemir](https://forum.inductiveautomation.com/u/ademovicemir)\
**Post date:** [August 5, 2023, 6:20pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/4 "2023-08-05T18:20:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ademovicemir](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ademovicemir](https://forum.inductiveautomation.com/u/ademovicemir)\
**Post date:** [August 5, 2023, 6:21pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/5 "2023-08-05T18:21:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ademovicemir](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ademovicemir](https://forum.inductiveautomation.com/u/ademovicemir)\
**Post date:** [August 7, 2023, 2:13pm UTC](https://forum.inductiveautomation.com/t/powertable-item-listener/77490/6 "2023-08-07T14:13:52Z")

</div>

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