Power Table Cell Editor Date Picker

I was able to configure a cell editor to use a date picker. However it throws an error: Can’t import name JCalendar which makes sense because it doesn’t exist. If I remove it, it then can’t find the JCalendar. If I leave it in the cell editor is indeed a calendar type date picker that works fine but throws that error when the table is first loaded. Can’t quite figure out this behavior.

	from javax.swing import AbstractCellEditor,JCalendar  
	from javax.swing.table import TableCellEditor
	
	if colName == "statusName":
		sel = "SELECT ID,Description FROM Change_Order_Statuses"
		pds = system.db.runQuery(sel)
		opts = []
		for row in pds:
			opts.append((row["ID"],row["Description"]))
		return {"options":opts}
	
	if colName == "StartDate":
		class cellEditor(AbstractCellEditor,TableCellEditor):
			def getTableCellEditorComponent(sel,table,value,isSelected,row,column):
				return JCalendar("A Calendar")
				
		return {'editor':cellEditor()}
1 Like

If the underlying field is an actual date type, the power table will show a calendar popup automatically when you set the field to Editable.

1 Like

Hey, due to a very specific set of circumstances, I need to create a date picker in a text column.

Can someone answer the OP’s question?

Here’s a method of doing this with credit to @nmudge for his approach binding a button to a power table column… but I think if you can avoid it and get the values cast to dates prior to populating your data set that would be a safer approach.

	# configureEditor() ->

	# magic date column
	if colName == "StartDate":
		from javax.swing import AbstractCellEditor
		from javax.swing.table import TableCellEditor
		from com.inductiveautomation.factorypmi.application.components import PMIDateTimePopupSelector
		
		def onDateChange(e):
			if e.propertyName == "date":
				self.data = system.dataset.setValue(self.data, self.selectedRow, self.selectedColumn, e.newValue)
						
		class MyCellEditor(AbstractCellEditor,TableCellEditor):
			def getTableCellEditorComponent(self,table,value,isSelected,row,column):
				cal = PMIDateTimePopupSelector()
				try:
					cal.setDate(system.date.parse(value))
				except:
					# handle blanks somehow -- this example sets to right now if edited cell is left blank
					cal.setDate(system.date.now())
				
				cal.addPropertyChangeListener(onDateChange)
				return cal
		return {'editor':MyCellEditor()}

Thanks for above solution.
Got this issue after using above mentioned code.

Exception in thread “AWT-EventQueue-0” NotImplementedError: ‘MyCellEditor’ object does not implement abstract method ‘getCellEditorValue’ from ‘javax.swing.CellEditor’

Kindly help on this to get solution.

Try adding these two lines in class MyCellEditor:

def getCellEditorValue(self):
     return self
1 Like