Scroll two (or more ) tables with one scroolbar

Hi all,

I wanted to be able to scroll ( vertically ) more tables based on just one scrollbar. I searched inside the forum but found nothing. Just some related posts. I took code from various posts and combined them together ( with no logic ) and I came up with the perfect result I wanted. Now, I am not a Java Expert ( I’m a Pythonian :slight_smile:), I was wondering if someone could help me figure out how this bunch of code is working.

Here it is:
I have 3 power tables called “Power Table”, “Power Table 1”, “Power Table 2”.
Inside the Mouse Clicked event of the Power Table I have the following code:

foo = event.source.getVerticalScrollBar()
table_2 = event.source.parent.getComponent('Power Table 1')
table_2.setVerticalScrollBar(foo)
table_3 = event.source.parent.getComponent('Power Table 2')
table_3.setVerticalScrollBar(foo)

Inside the Initialize event of the Power Table I have the following code:

	from java.awt.event import MouseWheelEvent
	from java.awt.event import MouseWheelListener
	
	class DebugMouseWheelListener(MouseWheelListener):
		def mouseWheelMoved(self, event):
			print "Scroll wheel notches moved:", event.getWheelRotation()
			print "Units to scroll:", event.getUnitsToScroll()
			
	self.addMouseWheelListener(DebugMouseWheelListener())

No code inside the others table.

Here there’s a video showing the result https:/uploads/iatesting/original/2X/a/a8545339f7e56263e556aafd258b1d205f4b40fb.mp4

The code in the initialize event isn’t doing anything related to what you have here. The part that’s doing anything is the mouse click event, although that’s probably not the best event handler to put this in.

foo = event.source.getVerticalScrollBar()
table_2 = event.source.parent.getComponent('Power Table 1')
table_2.setVerticalScrollBar(foo)
table_3 = event.source.parent.getComponent('Power Table 2')
table_3.setVerticalScrollBar(foo)

You’re taking the internal scroll bar component from one table, and telling the other two power tables to use the same scroll bar. That’s probably not the best/only way to do it, but if it’s working for you, by all means.

I would try putting the same code into the initialize() extension function (you can remove the other code there), and then replace event.source with self.

1 Like

I would add a ChangeListener to each table’s Viewport that would relay any desired operations to the other tables. And use mouseenter, mouseexit, and mousemove events to ensure only one table’s event are relayed at any given moment. The ChangeListener will get an event even if the scroll happens as a result of a non-mouse operation.

2 Likes

Thanks for the explanation! :slight_smile: