Vision Table Scrollbar Event

Is there a way to put a script on the scrollbar event of a table in vision? I would like to put a script on the table that would load more rows to the dataset when the user scrolls to about 75% of the way down.

For a granular hook, you would need to go from the component to its vertical scrollbar (literally getVerticalScrollBar(), then add an AdjustmentListener to it:
https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JScrollBar.html#addAdjustmentListener(java.awt.event.AdjustmentListener)

You'll want to do this only once, so in the initialize extension function of the table.

2 Likes

Note that a basic table doesn't have an initialize extension function, so to launch the script from there, a power table would need to be used instead. Of course, a listener could still be applied to a table, but it would probably need to be deployed from the parent window's internalFrameOpened event handler.
Here is a sample script that is written for a basic table, but it would work for a power table as well. The script finds the scrollbar from within the table, and adds the necessary listener. Afterwards, anytime a user scrolls more than 75% of the way through the table, the listener will add 10 blank rows to the table:

table = system.gui.getParentWindow(event).getComponentForPath('Root Container.Table')#For use from the internalFrameOpened event handler
from java.awt.event import AdjustmentListener
class scrollbarListener(AdjustmentListener):
	def adjustmentValueChanged(self, event):
		scrollPercentage = float(event.getValue())/float(event.source.getMaximum())
		if scrollPercentage > .75:#Adjust when to add rows here
			addRows()
def addRows():
	newRow = []
	for column in range(table.data.columnCount):
		newRow.append(None)
	for row in range(10):#Adjust the number of rows to add here
		table.data = system.dataset.addRow(table.data, table.data.rowCount, newRow)
scrollbar = table.getVerticalScrollBar()#getVerticalScrollbar()
adjustmentListener = scrollbarListener()
scrollbar.addAdjustmentListener(adjustmentListener)

Edit: replaced getScrollBar function with getVerticalScrollBar() method per Paul's suggestion which worked after correcting a capitalization error.

1 Like

Thanks for all the help. This seems to work for me on a power table initialize extension method

	from java.awt.event import AdjustmentListener
	class ScrollbarListener(AdjustmentListener):
		def adjustmentValueChanged(self, event):
			extent = event.source.getModel().getExtent()
			scrollBarPosition = event.source.getValue()+extent
			scrollBarPositionMax = event.source.getMaximum()
			scrollPerc = 0 if scrollBarPositionMax == 0 else scrollBarPosition/float(scrollBarPositionMax)
			
			if(scrollBarPosition != scrollBarPositionMax):
				inifinityScroll(scrollPerc)
	
	def inifinityScroll(scrollPerc):
		loadThresholdPerc = self.loadThresholdPerc/100.0
		if(scrollPerc >= loadThresholdPerc):
			newRow = []
			for columnNum in range(self.data.columnCount):
				newRow.append("data")
			for rowNum in range(10):#Adjust the number of rows to add here
				data = system.dataset.addRow(self.data, newRow)
			self.data = data
	
	vertScrollBar = self.getVerticalScrollBar()
	adjustmentListener  = ScrollbarListener()
	vertScrollBar.addAdjustmentListener(adjustmentListener)
1 Like

Nicely done. I see that you're using a custom float property 'loadThresholdPerc' to make the add point scriptable which makes sense, but the newRow.append("data") surprises me. Of course, if all of the columns are string columns this will never cause an issue.