Catching click and right-click events on power table headers

Is there a way to catch the click and/or right-click events on the headers of a power table?

Ideally, I’m looking for something like the “onPopupTrigger” extension function: I want to replace the default right-click menu with a menu where the user can sort and filter the data of a certain column, but where I keep control over the sorting by altering the source data (certain things need to stay in order).

But the “onPopupTrigger” only works on cells, not on table headers. Is there a way to catch this with generic mouse events? How do I know what column was clicked?

from java.awt.event import MouseAdapter

class TableHeaderMouseListener(MouseAdapter):
	def __init__(self,table):
		self.table = table
		
	def mouseClicked(self,e):
		point = e.getPoint()
		column = self.table.columnAtPoint(point)
		system.gui.messageBox("Clicked header %d"%column)

t = event.source.parent.getComponent('Power Table')
table = t.getTable()
header = table.getTableHeader()

# Remove any existing mouse listeners. 
mls = header.getMouseListeners()
for m in mls:
	header.removeMouseListener(m)
header.addMouseListener(TableHeaderMouseListener(table))
1 Like

I ended up putting this code in the “Initialise” function of the powertable:

	# put your initialization code here
	from java.awt.event import MouseAdapter
	import com.jidesoft.grid
	
	class TableHeaderMouseListener(MouseAdapter):
		def __init__(self,table):
			self.table = table
			
		def mouseClicked(self,e):
			self.table.headerClickHandler(e, "CLICKED")
			
		def mouseReleased(self, e):
			self.table.headerClickHandler(e, "RELEASED")
	
	table = self
	header = table.getTable().getTableHeader()
	
	# Remove any existing mouse listeners. 
	mls = header.getMouseListeners()
	for m in mls:
		if type(m) == com.jidesoft.grid.TableHeaderPopupMenuInstaller:
			header.removeMouseListener(m)
	header.addMouseListener(TableHeaderMouseListener(table))

This code only removes the popup menu listener, while it keeps other listeners (like the column resize listener). Then the code actually handling the clicks is defined in an separate function. The initialise function is only handled when the table is loaded for the first time, but that doesn’t matter here, as the bulk of code is defined in a different function which is reloaded on every call.

2 Likes