Scroll Event of a JScrollPane

aps picture aps · Jul 3, 2011 · Viewed 24.3k times · Source

It's strange that JScrollPane doesn't have a method addAdjustmentListener(). Yet adjustmentListener is said to be used with scrollbars. I want something to happen whenever the JScrollPane is scrolled in either way. How to achieve such a thing?

Answer

Jesse Barnum picture Jesse Barnum · Jul 3, 2011

Instead of trying to listen for changes from the JScrollPane, get the viewport for that scroll pane add a change listener to it:

http://download.oracle.com/javase/6/docs/api/javax/swing/JViewport.html#addChangeListener(javax.swing.event.ChangeListener)