2

I have a pane that contains image content which changes during scrolling. The content is properly updated via a scrollwheel event because I implemented a wheel listener which repaints the image before setting the new scroll value.

However, when the user drags the scrollbar handle with the mouse, the image content was not being updated during the manual drag-scroll. So I implemented a timer which grabs the current scroll value and repaints the content given the new scroll position.

This solution however (despite 10 millisecond adjustments) results in a jumpy scroll experience. The image moves (without the necessary image adjustments) and then gets corrected after-the-fact every 10 milliseconds.

I had originally tried an adjustmentlistener, but it only gets the event after the handle is released. How can I live-update the pane content during a jscrollbar handle drag BEFORE the scrollbar machinery starts to simply move my content as if it was a static image? Can I somehow give the scrollbar machinery a clue that content has changed or something every time it tries to redraw the content? Or can I disable the scrollbar's ability to move the image and just rely on my timer to do it?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
hepcat72
  • 890
  • 4
  • 22
  • Why not simply add a `ChangeListener` to the JScrollBar's BoundedRangeModel? For a more detailed answer, please consider creating and posting your [mcve]. – Hovercraft Full Of Eels Sep 02 '15 at 15:43
  • You add a mouse listener to the jscrollbar and use the mouseEntered method of the listener to respond. – c0der Sep 02 '15 at 15:50
  • I tried the ChangeListener suggestion and the stateChanged method only fires *after* the scroll handle is released, not during dragging, which is what I need. EDIT: Whoops, I did it wrong. You're right! This looks like what I need! Awesome. I'll post back if I can get it to work. I only tried debug prints thus far. – hepcat72 Sep 02 '15 at 16:12
  • Yup! ChangeListener did it! I was able to remove my timer and use the change listener to make the image adjustments. Now everything looks smooth as glass! You post an answer and I'll accept it! – hepcat72 Sep 02 '15 at 16:29

1 Answers1

1

I would recommend that you add a ChangeListener to the JScrollBar's model, a BounderedRangeModel, and then based on the value of the model as well as its maximum and minimum, change your image. If you're swapping images, the easiest way to do this is by swapping a JLabel's ImageIcon.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I'm not actually swapping an image. I'm drawing the content dynamically each time and it changes based on scroll position. But regardless, ChangeListener was exactly what I needed. All I had to do in stateChanged was initiate my image changes and then call a repaint. – hepcat72 Sep 02 '15 at 17:50