Synchronize the scroll of two data table contents together and ChangeListener

Hi all,

I have two tables which i would like to scroll synchronously.
I am using pturmel’s example (Freeze table column), and everything is working fine.
But I would like add some point to stop that ChangeListener and break synchronous scrolling two tables. Not sure how to do this. There is removeChangeListener() but i did not figure out how to use it.
Also, where can be found some details regarding viewport, especially for Table Component (table.viewport)?

Any help is appreciated, thanks!

You have to pass the listener you want to remove to the removeChangeListener() method. Which means you need to save it somewhere persistent. A client property of the component is probably the best place.

Thank you ptumel.

I tried client property. I added myScrollEvent to Button_1, and save it as myClientProperty_1, and then use another Button_2, get myClentProperty_1, print myScrollProperty_1 (<function myScrollEvent at 0x2>), but when i try to remove change listener “tblT22.viewport.removeChangeListener(myScrollEvent)”, nothing happens. Change listener is still active and both table are still scroll synchronously.
What i am doing wrong?

Ah, I think you’ll actually have to subclass the ChangeListener interface. I think that happens under the hood if you just add a callable, so the callable isn’t really the ChangeListener. If you subclass it, jython shouldn’t transform it automagically.

Thank you ptumel.
It works now. (had to search a little bit about sub-interfaces of EventListener)

2 Likes

Care to share the example for the community…?

hi zxcslo, here is what is use for testing synchronize the scroll of two tables:

tblT22 = event.source.parent.getComponent('Table 22')
tblT12 = event.source.parent.getComponent('Table 12')

class myListener(ChangeListener):
def stateChange(self, e):
scrollBarMain = tblT22.getHorizontalScrollBar()
scrollBarTop = tblT12.getHorizontalScrollBar()
scrollBarTop.setValue(scrollBarMain.getValue())

#to add ChangeListener
tblT22.viewport.addChangeListener(myListener())

#to remove ChangeListener
mls = tblT22.viewport.getChangeListeners()
for m in mls:
tblT22.viewport.removeChangeListener(m)

1 Like

where should i put this code?

If you're attempting this from Perspective, it won't work. In Vision, if you are using a Power Table, this will work in the initialize extension function, and if you are just using a regular Table, this will work from the componentRunning propertyChange event. [The componentRunning event will also work on the Power Table]

Notes:
• If you use theinitialize extension function, all instances of event.source will need to be replaced with self.
• If you are using the propertyChange event handler, you can use the code as is, but it needs to be nested in an if statement, or you'll end up with a million listeners on your tables:

if event propertyName == 'componentRunning':
     # nest the code here

• The componentRunning event doesn't occur in the designer unless preview mode is started before the window with the script is opened, and the only time I've seen the initialize extension function run in the designer has been when my Power Tables are loaded by a temple repeater or canvas , so testing in a session could be necessary for your user case.

Owh, i thought this is in perspective.. my bad