Setting Selected Row and Column on Table Right Click

On a table, with a left click, it will change the selected column and row. However, it does not do this with a right click. Here is the code to enable this. You will but this in event mouse click script.

[code]#Get the current viewport of the JScrollPane(the Component that holds the table)
view = event.source.viewport
#Get the current view position. This is in relation to top right hand corner of the component)
position = view.getViewPosition()

#Find the row that was selected. We subtract 17 as that is the height of the header
row = int((event.y-17+position.getY())/event.source.rowHeight)
#Set event.source.selectedRow to the row selected
event.source.selectedRow = row

#Get the Column Model of the table itself. Because there is only one component in the view, we use index 0
columnModel = view.getComponent(0).getColumnModel()
#Get the index of the column we selected
column = columnModel.getColumnIndexAtX(int(event.x+position.getX()))
#Set event.source.selectedColumn to the column selected
event.source.selectedColumn = column
[/code]

Here’s a different way to do the same thing:

[code]from java.awt import MouseInfo, Point
table = event.source
#get the mouse pointer location on the computer monitor
p = MouseInfo.getPointerInfo().getLocation()
#get the mouse pointer location relative to the Ignition application
mousex = p.x - table.getTable().getLocationOnScreen().x
mousey = p.y - table.getTable().getLocationOnScreen().y
mousep = Point(mousex,mousey)

#get the cellRow and cellCol and set them in the table
cellCol = table.getTable().columnAtPoint(mousep)
cellRow = table.getTable().rowAtPoint(mousep)
table.getTable().addColumnSelectionInterval(cellCol, cellCol)
table.getTable().addRowSelectionInterval(cellRow,cellRow)[/code]This goes in the mouseClicked event script on the table.