No worries; I figured out a way to automate the process. The following script checks to see if the Acked column is visible in the table, and if not, it programmatically adds the column. Afterwards, it programmatically sorts the table by that column, and if the acked column wasn't already visible, it removes the column immediately after the sorting is complete. Here is the code as fired from the actionPerformed script of my test button:
Edit: Updated script for latter versions of 8.1:
# Gets nested components using the class name
def getComponentOfClass(container, className):
for component in container.components:
if component.__class__.__name__ == className:
return component
else:
foundComponent = getComponentOfClass(component, className)
if foundComponent:
return foundComponent
alarmStatusTable = event.source.parent.getComponent('Alarm Status Table') #From a Test Button in the same container as the alarm status table
'''
alarmStatusTable = system.gui.getParentWindow(event).getComponentForPath('Root Container.Alarm Status Table') #From the parent window's internalFrameOpened event handler
alarmStatusTable = event.source #From the alarm status table's propertyChange event handler
alarmStatusTable = self #from the alarm status table's extension functions or custom methods
'''
#tableSorter(alarmStatusTable)
innerTable = getComponentOfClass(alarmStatusTable, 'AlarmStatusTable$1')
tableModel = innerTable.model
tableModel.sortColumn(tableModel.findColumn("Acked?"))
Older Version of the script
from javax.swing.table import TableColumn
def tableSorter(alarmStatusTable):
if alarmStatusTable.componentCount > 0:
for component in alarmStatusTable.getComponents():
if 'AlarmStatusTable$1' in str(component.__class__):
table = component
ackedColumn = None
rowSorterField = table.getClass().getSuperclass().getDeclaredField('Cc')
rowSorterField.setAccessible(True)
rowSorter = rowSorterField.get(table)
for column in range(component.columnCount):
if 'Acked' in component.getColumnName(column):
ackedColumn = column
columnModel = table.getColumnModel()
tableColumn = columnModel.getColumn(column)
rowSorter.sortColumn(tableColumn.getModelIndex())
break
if ackedColumn is None:
tableHeaders= table.getTableHeader()
columnModel = table.getColumnModel()
columnModel.addColumn(TableColumn(11))
tableColumn = columnModel.getColumn(column + 1)
tableColumn.setHeaderValue("Acked")
tableHeaders.repaint()
rowSorter.sortColumn(tableColumn.getModelIndex())
columnModel.removeColumn(tableColumn)#***Removes Acked Column after sorting
break
tableSorter(component)
alarmStatusTable = event.source.parent.getComponent('Alarm Status Table') #From a Test Button in the same container as the alarm status table
'''
alarmStatusTable = system.gui.getParentWindow(event).getComponentForPath('Root Container.Alarm Status Table') #From the parent window's internalFrameOpened event handler
alarmStatusTable = event.source #From the alarm status table's propertyChange event handler
alarmStatusTable = self #from the alarm status table's extension functions or custom methods
'''
tableSorter(alarmStatusTable)
Here is the result:
Edit:
• Corrected an erroneous assumption that the Acked Column would always be the last column in the table.
• Discovered that the acked column can be removed after sorting, and the table will still retain the sort order giving the illusion of default sort order. This was accomplished by adding columnModel.removeColumn(tableColumn) to the script.
• Gave the script a good refactor. It had originally been written for a much earllier version of ignition by a less experienced me
