Right-click a table component and select the row first

I have a table component with a popup menu on right click (using the mouseReleased event). When I right click on the table, it does not select the row that I am clicking, it just shows the popup menu. So if I have a different row selected and then right click and select an option from the popup menu, it is the wrong row that is affected. In order to affect the correct row, right now the steps are (1) left click the desired row, then (2) right click the table and select the option from the popup menu. I would like to only need to do step (2).

Is there a way for a right click to first select the row where the mouse was clicked, then do the popup menu? Thanks for your help!

I found this post which says how to use rowAtPoint(), but it doesn’t seem to account for scrolling. At the top of the table, the row index it returns is 1. If I scroll down and right click the top row that is now visible, it still returns 1.

So I suppose my new question is how to account for the fact that there are several rows that are not visible when I right click, since I have scrolled down?

If you can switch to a power table this is very easy to accomplish.

In the onPopupTrigger extension function simply add these 2 lines

if rowIndex > -1: self.selectedRow = rowIndex

If you have to stick with a standard table then I know it is possible to do but I don’t remember how at the moment. We used to have script in place to handle it before we switched over to the power table.

If you can’t move to a power table and nobody else posts a solution I can probably dig through our project archives and find an answer.

I figured it out using this StackOverflow question and this Java help page.

Here is what I have in my mouseReleased event handler.

if event.popupTrigger:
	#These seven lines select the correct row in the table.
	from java.awt import Point
	tbl = event.source
	jTbl = tbl.getTable()
	newY = jTbl.getVisibleRect().y
	oldPoint = event.getPoint()
	newPoint = Point(oldPoint.x, oldPoint.y + newY)
	tbl.setSelectedRow(jTbl.rowAtPoint(newPoint)-1)

	#These four rows make the right-click box appear on screen and display a message when selected.
	def popupMenuFunction(event):
		system.gui.messageBox("Test")
	menu = system.gui.createPopupMenu({'Test':popupMenuFunction})
	menu.show(event)

Since my table is only one column and does not ever scroll horizontally, I did not need to account for that.

JGJohnson thank you for your reply. I did want to avoid switching to a power table in this case if possible, but I appreciate your willingness to help.

For others looking at this post, with the help of Colton Connor (Inductive support), here is another solution to this:

Place the following inside the onPopupTrigger() extension function of the PowerTable

self.selectedRow = self.getTable().rowAtPoint(event.getPoint())

3 Likes