Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll Event of a JScrollPane

Tags:

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?

like image 744
aps Avatar asked Jul 03 '11 05:07

aps


People also ask

What is a JScrollPane?

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split panes and tabbed panes.

What is a JScrollBar and a JScrollPane?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.


2 Answers

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)

like image 182
Jesse Barnum Avatar answered Oct 27 '22 00:10

Jesse Barnum


Why don't you get the actual scrollbars, and then add adjustment listeners to them? JScrollBar has a method called addAdjustmentListener().

And you can use getVerticalScrollBar() and getHorizontalScrollBar() to get a JScrollBar from a JScrollPane

like image 43
Michael Studebaker Avatar answered Oct 27 '22 01:10

Michael Studebaker